I need help with my wordpress site. While displaying "tags" on a product page,
<?php echo $product->get_tags(', ', '<span class="tagged_as">' . _n( 'Tag:', 'Tags:', $tag_count, 'woocommerce' ) . ' ', '</span>' ); ?>
is used to show them.
However, I need product page to show only 1 tag for a product. This code makes all tags to appear on the page. Which code should I use or how should I edit this?
in example: if a product has the tags: summer, winter, fall, spring I want only "fall" to be seen
<?php
$tags = get_terms( 'product_tag' );
$first = true;
foreach ($tags as $tag) {
if ( $first ){
$term_link = get_term_link( $tag );
if ( is_wp_error( $term_link ) ) {continue;}
echo '<a href="' . esc_url( $term_link ) . '">' . $tag->name . '</a>';
$first = false;
}
}
?>
This will only print the first tag in a woocommerce product page. Its tested and working.
If you want to display any selected tag, then you need to define which tag you want to be visible. Maybe using a custom field. Lets say you have a custom field name 'selected_tag' using ACF plugin and it will print the exact tag name that you want to visible in the product page.
<?php
$selected_tag = get_field('selected_tag');
$tags = get_terms( 'product_tag' );
foreach ($tags as $tag) {
if ( $tag->name == $selected_tag ){
$term_link = get_term_link( $tag );
if ( is_wp_error( $term_link ) ) {continue;}
echo '<a href="' . esc_url( $term_link ) . '">' . $tag->name . '</a>';
}
}
?>