【问题标题】:Hide subcategories from woocommerce category widget从 woocommerce 类别小部件中隐藏子类别
【发布时间】:2015-12-01 21:54:47
【问题描述】:

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

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

链接:http://tithaty.com.br/?post_type=product

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

Colecao teste 是一个子类别的例子。

有什么想法吗?

谢谢

【问题讨论】:

    标签: wordpress widget woocommerce categories


    【解决方案1】:

    您需要稍微修改过滤器代码。我在代码中放置了 cmets 以帮助您了解它是如何工作的。代码将确保 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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-12-04
      • 1970-01-01
      • 2022-12-10
      • 1970-01-01
      • 2018-03-03
      • 2021-04-19
      • 1970-01-01
      相关资源
      最近更新 更多