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

Hide WooCommerce specific sub-category in single product pages list output

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

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

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

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

任何关于从哪里开始的想法and/or如何去做,我们将不胜感激。

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

  • 产品类别 "In Ground Hoops" 术语别名是:'in-ground-hoops'
  • 产品子类别 "In Ground Hoops" 词条是:'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 文件。

已测试并有效。