gpt4 book ai didi

php - Woocommerce 类别显示 : Show subcategories & products excluding subcategory products

转载 作者:行者123 更新时间:2023-12-05 06:35:19 24 4
gpt4 key购买 nike

我需要一些有关 Woocommerce 类别显示选项的帮助。在 Wordpress 仪表板中,我按照以下链接全局设置类别显示选项。
外观 -> 自定义 -> Woocommerce -> 产品目录 -> 类别展示

共有三种显示选项。

  1. 展示产品
  2. 显示子类别
  3. 显示子类别和产品

目前选择的是第三个选项,显示的是子类目以及父类目和子类目下的所有商品。但是,我想排除子类别下的所有产品。换句话说,我只想显示父类别下的子类别和产品。

我在几个教程网站上找到了以下片段。该代码完全按照我想要的方式工作,但它也禁用了管理产品搜索和过滤功能(无论如何它都不返回任何结果)。
知道为什么会这样吗?

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/主题:Storefront v2.2.8

最佳答案

为避免此问题,请在以下代码之间插入脚本:if ( !is_admin() ) { }

完整代码:

    if ( ! is_admin() ) {
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');

}

关于php - Woocommerce 类别显示 : Show subcategories & products excluding subcategory products,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49713993/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com