隐藏woocommerce类别小部件中的子类别 [英] Hide subcategories from woocommerce category widget

查看:99
本文介绍了隐藏woocommerce类别小部件中的子类别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用内置的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 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆