最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin8PV0评论

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

Post a comment

comment list (0)

  1. No comments so far