最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

woocommerce offtopic - Create a shortcode to show product tag name on tag archive

programmeradmin0浏览0评论

I'm trying to build a global description for my product tags. So they will have a general description with shortcodes on it showing the exact tag name.

For example: Buy [tag_name] products

I'm trying to get the tag name and create the shortcode by this code:

function displayMotorcycleName($item) {
    $productTag = get_the_terms( get_the_ID(), 'product_tag' );
    return $productTag;
}

add_shortcode('product_tags', 'displayMotorcycleName');

However it's showing the word "Array" instead of the tag name.

Any suggestions? Thanks!

I'm trying to build a global description for my product tags. So they will have a general description with shortcodes on it showing the exact tag name.

For example: Buy [tag_name] products

I'm trying to get the tag name and create the shortcode by this code:

function displayMotorcycleName($item) {
    $productTag = get_the_terms( get_the_ID(), 'product_tag' );
    return $productTag;
}

add_shortcode('product_tags', 'displayMotorcycleName');

However it's showing the word "Array" instead of the tag name.

Any suggestions? Thanks!

Share Improve this question asked Apr 4, 2019 at 17:12 Felipe MichelinFelipe Michelin 1 1
  • Hi Felipe. You should check here to see how to use get_the_terms. It does return an array and you will likely want to loop through each of the results. developer.wordpress/reference/functions/get_the_terms – rudtek Commented Apr 5, 2019 at 17:05
Add a comment  | 

1 Answer 1

Reset to default 2

Because the return value from get_the_terms() is an array, not a string. See the Codex https://codex.wordpress/Function_Reference/wp_get_post_terms for the array values returned.

To return the 'name' element of the array , use

return $productTag['name'];

(The Codex is a great place to figure out what a function does....)

发布评论

评论列表(0)

  1. 暂无评论