仅在WooCommerce中获取当前产品的产品标签 [英] Get the Product tags for the current product only in WooCommerce

查看:395
本文介绍了仅在WooCommerce中获取当前产品的产品标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何仅显示当前单个产品页面的产品标签,而不显示所有产品标签?

How can I display only the product tags for the current single product page and not all the products tags?

我发现了有关最受欢迎标签的问题,但没有找到

I've found questions about most popular tags but not for that.

推荐答案

您可以使用函数 wp_get_post_terms()用于WooCommerce'product_tag'自定义分类法和定义的产品ID 的函数,如下所示:

You can use the function wp_get_post_terms() function for WooCommerce 'product_tag' custom taxonomy and a defined product id this way:

$output = array();

// get an array of the WP_Term objects for a defined product ID
$terms = wp_get_post_terms( get_the_id(), 'product_tag' );

// Loop through each product tag for the current product
if( count($terms) > 0 ){
    foreach($terms as $term){
        $term_id = $term->term_id; // Product tag Id
        $term_name = $term->name; // Product tag Name
        $term_slug = $term->slug; // Product tag slug
        $term_link = get_term_link( $term, 'product_tag' ); // Product tag link

        // Set the product tag names in an array
        $output[] = '<a href="'.$term_link.'">'.$term_name.'</a>';
    }
    // Set the array in a coma separated string of product tags for example
    $output = implode( ', ', $output );

    // Display the coma separated string of the product tags
    echo $output;
}

经过测试并有效。

您也可以用动态产品ID变量替换 get_the_id()

You can replace get_the_id() by a dynamic product Id variable too.

这篇关于仅在WooCommerce中获取当前产品的产品标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆