【问题标题】:Hiding WooCommerce Categories that don't have any products in them?隐藏其中没有任何产品的 WooCommerce 类别?
【发布时间】:2026-01-29 19:15:02
【问题描述】:

我想禁用其中没有任何产品的类别。这是似乎不起作用的代码。

放在我的functions.php中

 function woo_hide_product_categories_widget( $list_args ){
$list_args[ 'hide_empty' ] = 1;

return $list_args;
 }
add_filter( 'woocommerce_product_categories_widget_args','woo_hide_product_categories_widget' );

【问题讨论】:

    标签: php css wordpress woocommerce


    【解决方案1】:

    add_filter('woocommerce_product_categories_widget_args', 'wpsites_exclude_product_cat_widget');

    函数 wpsites_exclude_product_cat_widget( $args ) {

    $args['exclude'] = array('16','46');

    返回 $args; }

    试试这个

    【讨论】:

    • 现在有什么问题?
    • 它没有隐藏未使用的类别。
    • 通过 id 获取您的类别并简单地传递到数组中
    【解决方案2】:

    您可以通过以下方式隐藏特定类别:

    add_action( 'pre_get_posts', 'uw_remove_product_cats_shop_page' );
    function uw_remove_product_cats_shop_page( $query ) {
    
        // Comment out the line below to hide products in the admin as well
        if ( is_admin() ) return;
    
        if ( is_shop() && $query->is_main_query() ) {
    
            $query->set( 'tax_query', array(
                array(
                    'taxonomy' => 'product_cat',
                    'field' => 'ID',
                    'terms' => array( 200, 205, 210 ), //ID of categories here
                    'operator' => 'NOT IN'
                )
            ) );
    
        }
    
    }
    

    【讨论】: