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

I'm trying to build a global description for my product tags.So they will have a general description with shortcod

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....)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745628216a4636940.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信