在 Woocommerce 的宏观类别中隐藏子类别产品

问题描述:

我想展示一个宏观类别的产品和子类别,但我不想展示子类别的产品.

I would like to show products and subcategories of a macro-category but I DON'T want to show the products of the subcategories.

这是我的问题的一个例子:http://www.idromet.it/jml/wp/categoria-prodotto/prodotti/tubi-raccordi-acciaio-al-carbonio/

Here's an example of my problem: http://www.idromet.it/jml/wp/categoria-prodotto/prodotti/tubi-raccordi-acciaio-al-carbonio/

"Raccordi in ghisazingati" 显示 2 次,因为第一个是类别(及其右侧),第二个是是该子类别的产品(和我不想在这里展示).

"Raccordi in ghisa zincati" is showed 2 times because the first is the category (and its right), the second one is the product of that sub-category (and I don't want to show it in here).

下面的代码应该粘贴到子主题文件夹下的functions.php文件中.

The code below should be pasted in the functions.php file located in your child theme folder.

function exclude_product_cat_children($wp_query) {
if ( isset ( $wp_query->query_vars['product_cat'] ) && $wp_query->is_main_query()) {
    $wp_query->set('tax_query', array( 
                                    array (
                                        'taxonomy' => 'product_cat',
                                        'field' => 'slug',
                                        'terms' => $wp_query->query_vars['product_cat'],
                                        'include_children' => false
                                    ) 
                                 )
    );
  }
}  
add_filter('pre_get_posts', 'exclude_product_cat_children');