Hide subcategories from woocommerce category widget

Deadly 提交于 2021-01-28 06:21:51

问题


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.

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

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 is an example of a subcategory.

Any ideas?

Thank you


回答1:


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


来源:https://stackoverflow.com/questions/32420094/hide-subcategories-from-woocommerce-category-widget

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!