admin管理员组

文章数量:1315778

I have a site with many product categories and subcategories. I need to show the same sidebar as the primary category in the subcategories as well.

Example:

Wine (sidebar 1) White Wine (sidebar 1) Red Wine (sidebar 1) ...and many others

Distillates (sidebar 2) Whisky (sidebar 2) Brandy (sidebar 2) ...and many others

So I need a system that is as automatic as possible.

if ( is_product_category( 'wine' ) ) {
dynamic_sidebar('Wine Sidebar');
}
elseif ( is_product_category( 'distillates' ) ) {
dynamic_sidebar('Distillates Sidebar');
}

This code is only for primary categories.

I have a site with many product categories and subcategories. I need to show the same sidebar as the primary category in the subcategories as well.

Example:

Wine (sidebar 1) White Wine (sidebar 1) Red Wine (sidebar 1) ...and many others

Distillates (sidebar 2) Whisky (sidebar 2) Brandy (sidebar 2) ...and many others

So I need a system that is as automatic as possible.

if ( is_product_category( 'wine' ) ) {
dynamic_sidebar('Wine Sidebar');
}
elseif ( is_product_category( 'distillates' ) ) {
dynamic_sidebar('Distillates Sidebar');
}

This code is only for primary categories.

Share Improve this question edited Nov 18, 2020 at 14:38 AndreaLB 174 bronze badges asked Nov 18, 2020 at 0:18 AndreaAndrea 111 bronze badge 1
  • You can do it as an array, so you generate your list of categories, both parents and children, and then instead of checking just one, you check the entire array... Are you manually inputting the categories or are you programmatically calling them? I'll post an answer. – Tony Djukic Commented Nov 18, 2020 at 3:52
Add a comment  | 

2 Answers 2

Reset to default 1
$currentCat = get_queried_object();
if ( $currentCat->term_id == 64 || $currentCat->parent == 64 ) { 
dynamic_sidebar('Distillati Sidebar');
}

I solved in this way: it works!

Not sure how you're going to be calling these categories as you could do it programmatically using 'child_of'=>$parentCatID but you'd need to check the WooCommerce documentation to see exactly how.

To answer your question, is_product_category() will accept an array of categories, so the following should work:

if( is_product_category( array( 'wine', 'white-wine', 'red-wine' ) ) ) :
     dynamic_sidebar( 'Wine Sidebar' );
elseif( is_product_category( array( 'distillates', 'whiskey', 'brandy' ) ) ) :
     dynamic_sidebar( 'Distillates Sidebar' );
endif;

Hope that helps and welcome to WordPress.StackExchange. :-)

本文标签: Dynamic Sidebar for subcategory of a category