duanlvxing7707 2018-04-08 03:05
浏览 407

Woocommerce类别显示:显示子类别和产品,不包括子类别产品

I need some help on category display option for Woocommerce. In the Wordpress dashboard, I followed the below links to set up the category display option globally.
Appearance -> Customize -> Woocommerce -> Product Catalog -> Category display

There are three display options available.

  1. Show products
  2. Show subcategories
  3. Show subcategories & products

Currently, the third option is selected and it shows subcategories and all the products under parent categories and subcategories. However, I want to exclude all the products under subcategories. In other words, I want to show subcategories and products under parent categories only.

I found the following snippet at several tutorial sites. The code works exactly the way I want but it also disables the admin products search and filter function (it returns no results regardless).
Any idea why it happens?

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');

WP: v4.9.5 / Woocommerce: v3.3.4 / Theme: Storefront v2.2.8

  • 写回答

2条回答 默认 最新

  • dqnhfbc3738 2018-08-02 14:55
    关注

    Try this, it works for me

    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');
    
    评论

报告相同问题?

悬赏问题

  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services