从 woocommerce 类别小部件中隐藏子类别 [英] Hide subcategories from woocommerce category widget
问题描述
我正在使用内置的 woocommerce 类别小部件,目前它同时显示类别和子类别.
I'm using the built-in woocommerce category widget, and at the moment it's displaying both the categories and subcategories.
我通过此代码排除了一个类别:
I excluded a category via this code:
add_filter( 'woocommerce_product_categories_widget_args', 'organicweb_exclude_widget_category' );
function organicweb_exclude_widget_category( $args ) {
// Enter the id of the category you want to exclude in place of '30'
$args['exclude'] = array('62' );
return $args;
}
但小部件仍显示它的子类别.
but the widget still shows it's subcategories.
链接:http://tithaty.com.br/?post_type=product
隐藏的类别是 Coleções(我配置为父类别),我想隐藏它的子类别、当前类别和将来添加的类别.
The category hidden is Coleções ( I configured as parent) and I want to hide it's subcategories, current and the ones added in the future.
Colecao teste 是一个子类别的例子.
Colecao teste is an example of a subcategory.
有什么想法吗?
谢谢
推荐答案
需要稍微修改一下过滤器代码.我在代码中添加了注释以帮助您了解它是如何工作的.代码将确保 Coleções 的现有子类别和将来添加的子类别始终处于隐藏状态.
You need to modify the filter code a bit. I have placed comments in the code to help you understand how it works. Code will ensure that existing sub-categories of Coleções and the ones added in future are always hidden.
add_filter( 'woocommerce_product_categories_widget_args', 'organicweb_exclude_widget_category' );
function organicweb_exclude_widget_category( $args ) {
// Create an array that will hold the ids that need to be excluded
$exclude_terms = array();
// Push the default term that you need to hide
array_push( $exclude_terms, 62 );
// Find all the children of that term
$termchildren = get_term_children( 62, 'product_cat' );
// Iterate over the terms found and add it to the array which holds the IDs to exclude
foreach( $termchildren as $child ) {
$term = get_term_by( 'id', $child, 'product_cat' );
array_push( $exclude_terms, $term->term_id );
}
// Finally pass the array
$args['exclude'] = $exclude_terms;
return $args;
}
这篇关于从 woocommerce 类别小部件中隐藏子类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!