也想出现在这里?联系我们

在WooCommerce父类产品列类表中隐藏子类产品

2023.12.30 wordpress教程
  • 文章介绍
  • 升级版本
  • 评价&建议

要在WooCommerce中隐藏父类产品列表中的子类产品,您可以使用以下方法之一:

  1. 使用代码:

    您可以通过添加一些自定义代码来实现此目标。首先,您需要在您的主题的functions.php文件中添加以下代码:

    function exclude_child_categories_from_shop($query) {
       if (is_admin() || !is_shop()) return $query;
    
       $current_category = get_queried_object();
    
       if ($current_category && $current_category>parent) {
           $term_ids = array($current_category>term_id);
           $child_terms = get_terms(array(
               'taxonomy' => 'product_cat',
               'child_of' => $current_category>term_id,
           ));
    
           foreach ($child_terms as $child_term) {
               $term_ids[] = $child_term>term_id;
           }
    
           $query>set('product_cat', implode(',', $term_ids));
       }
    
       return $query;
    }
    
    add_filter('pre_get_posts', 'exclude_child_categories_from_shop');

    这段代码会检查当前页面是否为商店页面,并且如果是的话,它会排除子类别产品。您只需将此代码添加到您的主题的functions.php文件中,然后保存文件即可。

  2. 使用插件:

    您还可以考虑使用WooCommerce插件,如"Hide Products by Category"或"Hide Categories on Shop Page"。这些插件可以让您在WordPress后台轻松设置产品隐藏规则,而无需编写任何代码。您可以在WordPress插件市场中搜索这些插件并安装它们。

    请注意,插件的功能和界面可能会有所不同,具体取决于您选择的插件,因此您需要根据插件的文档或设置来配置它们以实现您的目标。

不管您选择哪种方法,都应该能够在WooCommerce中轻松隐藏子类别产品。

有用0
  • 2023.12.30初次和大家见面了!

等待您对该主题的建议

发表评论

还能输入240个字

Hi, 欢迎加入Wordpress技术交流群,带你装逼带你飞!

我要入群
也想出现在这里?联系我们
wordpress加速

我来推荐一个更牛逼的给你看看?

  • 猛戳我吧