As the title says I’m looking for a shortcode I can use to show the tag of a specific product. All my products have only one product tag set for each.
For example, if the product with ID: 1250 has the tag “Horse” I need the way to put a shortcode specifying the ID of the product and show your respective tag. In the example the shortcode should show on the screen the word “Horse”
Tyring to modify the following code to achieve it:
$terms = wp_get_post_terms( get_the_id(), 'product_tag' );
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' );
$output[] = '.$term_name.';
}
$output = implode( ', ', $output );
echo $output;
}
But I don't have the enough knowledge to achieve it
Any help is appreciated.
If you have only one product tag set for each product, the following shortcode function will output the product tag term name set for the current product (or a string of coma separated term names, when there are multiple terms set for a product). It works also for a defined product Id as argument in the shortcode (see usage examples).
The function code:
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
USAGE examples:
[wc_product_tag]
or in php:
echo do_shortcode('[wc_product_tag]');
[wc_product_tag product_id="37"]
or in php:
echo do_shortcode('[wc_product_tag product_id="37"]');
To display WooCommerce product category term names set for a product you need to replace:
by the following line: