在 Woocommerce 的宏观类别中隐藏子类别产品 [英] Hide subcategories products in the macro-category in Woocommerce

查看:29
本文介绍了在 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'); 

这篇关于在 Woocommerce 的宏观类别中隐藏子类别产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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