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
2 Answers
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
版权声明:本文标题:Dynamic Sidebar for subcategory of a category 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741984207a2408575.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论