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
1 Answer
Reset to default 2Because 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....)