从 woocommerce 类别小部件中隐藏子类别

Hide subcategories from woocommerce category widget

我正在使用内置的 woocommerce 类别小部件,目前它同时显示类别和子类别。

我通过此代码排除了一个类别:

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;
}

但是小部件仍然显示它的子类别。

link: http://tithaty.com.br/?post_type=product

隐藏的类别是 Coleções(我配置为父级),我想隐藏它的子类别,当前的和将来添加的子类别。

Colecao teste 是子类别的示例。

有什么想法吗?

谢谢

您需要稍微修改一下过滤代码。我在代码中放置了注释以帮助您了解它是如何工作的。代码将确保 Coleções 的现有子类别和将来添加的子类别始终隐藏。

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;
}