admin管理员组文章数量:1289866
I've been working on trying to hide all other categories from the category widget when a particular category is selected.
So, as an example, the site has 5x Products categories and and 1x Services category. When browsing the Services I do not want anything from the 5x products categories to display in the categories widget. The below code works fine to hide the 5x product categories but it hides it site wide, which I don't want.
add_filter( 'woocommerce_product_categories_widget_args', 'exclude_widget_category' );
function exclude_widget_category( $args ) {
$exclude_terms = array();
array_push( $exclude_terms, 2548, 2245, 2775, 2913, 2846 );
$termchildren = get_term_children( '2548, 2245, 2775, 2913, 2846', 'product_cat' );
foreach( $termchildren as $child ) {
$term = get_term_by( 'id', $child, 'product_cat' );
array_push( $exclude_terms, $term->term_id );
}
$args['exclude'] = $exclude_terms;
return $args;
}
So to target the Services cat I added is_product_category and this works OK when on the services category but if i'm in a products category it completely breaks the page (no products appear, no categories etc). It also results in all subcategories in Services behaving like that too.
if ( is_product_category(2921) ) {
(2921 is my services category and 2548, 2245, 2775, 2913, 2846 are my product categories)
Also, I may as well add this now; need this to work the other way as well, so if you are in any of the products categories then the services categories should be hidden.
What am I doing wrong?
Many thanks.
I've been working on trying to hide all other categories from the category widget when a particular category is selected.
So, as an example, the site has 5x Products categories and and 1x Services category. When browsing the Services I do not want anything from the 5x products categories to display in the categories widget. The below code works fine to hide the 5x product categories but it hides it site wide, which I don't want.
add_filter( 'woocommerce_product_categories_widget_args', 'exclude_widget_category' );
function exclude_widget_category( $args ) {
$exclude_terms = array();
array_push( $exclude_terms, 2548, 2245, 2775, 2913, 2846 );
$termchildren = get_term_children( '2548, 2245, 2775, 2913, 2846', 'product_cat' );
foreach( $termchildren as $child ) {
$term = get_term_by( 'id', $child, 'product_cat' );
array_push( $exclude_terms, $term->term_id );
}
$args['exclude'] = $exclude_terms;
return $args;
}
So to target the Services cat I added is_product_category and this works OK when on the services category but if i'm in a products category it completely breaks the page (no products appear, no categories etc). It also results in all subcategories in Services behaving like that too.
if ( is_product_category(2921) ) {
(2921 is my services category and 2548, 2245, 2775, 2913, 2846 are my product categories)
Also, I may as well add this now; need this to work the other way as well, so if you are in any of the products categories then the services categories should be hidden.
What am I doing wrong?
Many thanks.
Share Improve this question edited Jul 5, 2021 at 17:56 Forau asked Jul 5, 2021 at 17:41 ForauForau 13 bronze badges 5 |1 Answer
Reset to default 0Went about it a different way, works OK.
Just in case anyone else is looking for similar functionality;
//* dropdown
add_filter( 'woocommerce_product_categories_widget_dropdown_args', 'woo_product_cat_widget_args' );
//* list
add_filter( 'woocommerce_product_categories_widget_args', 'woo_product_cat_widget_args' );
function woo_product_cat_widget_args( $cat_args ) {
if ( is_product_category( 'experiences' ) || term_is_ancestor_of( 2921, get_queried_object_id(), 'product_cat' ) ) {
$cat_args['exclude'] = array(2548, 2245, 2775, 2913, 2846);
return $cat_args;
}
else {
$cat_args['exclude'] = array('2921');
return $cat_args;
}
}
本文标签: categoriesAdd filter for specific category only
版权声明:本文标题:categories - Add filter for specific category only 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741422008a2377860.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
is_product_category
is incomplete so it's unclear what is inside the if statement, but I suspect you are unaware that filters always return something, and your filter only returns in the scenario you wanted to make a modification. When it is not that scenario you're supposed to return it unchanged/unmodified. If you do not return a value, it doesn't just use the original, it uses an empty undefined value, aka nothing – Tom J Nowell ♦ Commented Jul 5, 2021 at 18:00woocommerce_product_categories_widget_args
filter, is that a WooCommerce specific filter? 3rd party plugin dev support is off topic here, if you need help with WooCommerce you should ask via their support routes, e.g. slack/forums/FB group/etc – Tom J Nowell ♦ Commented Jul 5, 2021 at 18:01