【问题标题】:Hide WooCommerce specific sub-category in single product pages list output在单个产品页面列表输出中隐藏 WooCommerce 特定子类别
【发布时间】:2018-06-10 08:07:30
【问题描述】:

在 WooCommerce 单个产品页面上,添加到购物车按钮下显示产品类别列表。

In my website 我有 2 个名为“In Ground Hoops”的产品类别,所以它显示了两次,因为它是一个主类别和一个子类别。

我怎样才能隐藏这个子类别不在那里显示?

我无法使用 CSS 来定位它,而 woocommerce_template_single_meta 钩子是我能找到的全部或全部。

任何关于从哪里开始和/或如何做的想法都将不胜感激。

【问题讨论】:

    标签: php wordpress woocommerce categories product


    【解决方案1】:

    您的产品类别和子类别“In Ground Hoops”具有相同的术语名称,但每个术语的名称不同:

    • 产品类别“在地箍中”术语 slug 是:'in-ground-hoops'
    • 产品子类别“在地箍中”术语 slug 是:'in-ground-hoops-goalrilla'

    因此,在代码中区分它们的唯一方法是通过它们的term ID 或它们的term slug。所以我将在这里使用 术语 Slug...

    查看显示元输出的 the responsible template 时,过滤该特定产品子类别的唯一方法是使用可用的 WP get_the_terms 过滤钩子:

    add_filter( 'get_the_terms', 'hide_specific_product_subcategory', 20, 3 );
    function hide_specific_product_subcategory( $terms, $post_id, $taxonomy )
    {
        // Only on single product pages and for product category custom taxonomy
        if( ! is_product() && $taxonomy != 'product_cat' ) return $terms; // We Exit
    
        $category = 'in-ground-hoops'; // your main product category
        $subcategory = 'in-ground-hoops-goalrilla'; // the related subcategory
        $taxonomy = 'product_cat'; // product category taxonomy
    
        if( has_term( $category, $taxonomy, $post_id ) ){
            foreach ($terms as $key => $term){
                if( $term->slug == $subcategory ){
                    unset( $terms[$key] );
                }
            }
        }
        return $terms;
    }
    

    代码进入您的活动子主题(或活动主题)的 function.php 文件中。

    经过测试并且有效。

    【讨论】:

      猜你喜欢
      • 2019-06-23
      • 2018-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-12
      • 2020-08-21
      • 2022-08-24
      相关资源
      最近更新 更多