隐藏woocommerce类别小部件

时间:2015-09-06 04:35:27

标签: wordpress widget woocommerce categories

我正在使用内置的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是一个子类别的例子。

有什么想法吗?

谢谢

1 个答案:

答案 0 :(得分:2)

您需要稍微修改过滤器代码。我在代码中添加了注释,以帮助您了解它的工作原理。代码将确保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;
}