在Woocommerce产品类别小部件中显示子类别归档的子级
我正在使用WooCommerce建立商店,并使用WooCommerce产品类别窗口小部件.我用子类别设置了许多产品类别.这些类别之一是海报",并且有几个子类别,例如星标",旅行",自然" ...
I'm building a shop with Woocommerce and using WooCommerce Product Category widget. I have set many product categories with subcategories. One of these categories is "Posters" and has several subcategories, like "Star signs", "Travel", "Nature"…
默认情况下,Woocommerce只显示父类别,这很好.如果单击海报"类别,则会重定向到海报"类别存档页面,并且该小部件将显示所有海报"子类别,这是完美的.
By default Woocommerce only shows the parent categories which is good. If I click a category "posters", I'm redirected to "posters" category archive page and the widget displays all "Posters" children subcategories and it's perfect.
现在,如果我单击这些海报"子类别中的一个,我将被重定向到相应的存档页面,但是导航不再显示所有其他海报"子类别.
Now, if I click on one of these "Posters" children subcategories, I'm redirected to the respective archive page, but my navigation doesn't show all other "Posters" children subcategories anymore.
问题:
浏览子类别时如何获取所有父类别和同级"子类别?
The question:
How do I get all parent category and "sibling" subcategories while browsing a subcategory?
随机示例:
手机套
杯子
枕头
海报
衬衫
贴纸
Phone Cases
Mugs
Pillows
Posters
Shirts
Stickers
导航示例,当点击海报"时:
手机套
杯子
枕头
海报
-星空标志
-旅行
-自然
-摘要
-版式
衬衫
贴纸
Phone Cases
Mugs
Pillows
Posters
--Star Signs
--Travel
--Nature
--Abstract
--Typography
Shirts
Stickers
当点击子类别时,例如自然",导航返回,看起来像给定的第一个示例,全部折叠.但我希望它像第二个示例一样保持扩展.
When a subcategory is clicked, e.g. "Nature", the navigation returns to look like the first example given, all collapsed. But I want it to stay expanded like in the second example.
屏幕快照中的下面是我对Woocommerce产品类别窗口小部件的设置:
Bellow in the screenshot are my settings for Woocommerce Product Category widget:
任何帮助将不胜感激.
您的设置正确.下面的代码仅定位到存档类别页面,现在将在Woocommerce产品类别窗口小部件中显示当前子类别的所有子类别:
Your settings are correct. The code below is targeting archives category pages only and it will display now all children subcategories in the Woocommerce Product Category widget, for the current subcategory:
add_filter('woocommerce_product_categories_widget_args', 'widget_product_categories_list_args', 10, 1);
function widget_product_categories_list_args( $list_args ) {
global $wp_query;
// Only for category archives pages
if ( is_tax( $list_args['taxonomy'] ) ):
// Get current category
$current_cat = $wp_query->queried_object;
// Get all Included category terms IDs in the widget
$included_ids = explode( ',', $list_args['include'] );
// Get All Childrens Ids from parent term or from current term
if($current_cat->parent != 0 )
$childrens = get_term_children( $current_cat->parent, $list_args['taxonomy'] );
else
$childrens = get_term_children( $current_cat->term_id, $list_args['taxonomy'] );
// Loop through Children term Ids and add them to existing included ones
foreach( $childrens as $child )
$included_ids[] = $child;
// Replace included product category term IDs in the $args array
$list_args['include'] = $included_ids;
endif;
return $list_args;
}
代码进入活动子主题(或活动主题)的function.php文件.
经过测试,可以正常工作.
Tested and works.