首页 文章

在单个产品页面列表输出中隐藏WooCommerce特定子类别

提问于
浏览
2

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

In my website我有2个名为"In Ground Hoops"的产品类别,因此它会显示两次,因为它是主要类别和子类别 .

如何隐藏此子类别不显示在那里?

我不能用CSS定位它, woocommerce_template_single_meta 钩子是我能找到的全部或全部 .

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

1 回答

  • 1

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

    • 产品类别"In Ground Hoops" term slug是: 'in-ground-hoops'

    • 产品子类别"In Ground Hoops" term slug是: 'in-ground-hoops-goalrilla'

    因此,在代码中区分它们的唯一方法是 term IDterm slug . 所以我会在这里使用 term Slug ...

    查看 the responsible template that is displaying the meta output 时,过滤该特定产品子类别的唯一方法是使用可用的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文件中 .

    经过测试和工作 .

相关问题