I've been using product tags as brand for products in my shop. The following code which I have found online has been working fine (until the recent update of woocommerce and WP) to make a list on my "brands" page. It lists all brands under each specific category letter.
Suddenly no URLs are being generated, everything else is populated as it should. I really can't find the issue. I have tried to modify get_terms
to get_terms(array('taxonomy'=>'product_tag'))
but it's not working either.
This is my code:
$list = '';
$tags = get_terms( 'product_tag' );
$groups = array();
if( $tags && is_array( $tags ) ) {
foreach( $tags as $tag ) {
$first_letter = strtoupper( $tag->name[0] );
$groups[ $first_letter ][] = $tag;
}
if( !empty( $groups ) ) {
foreach( $groups as $letter => $tags ) {
$list .= "\n\t" . '<div class="brand_category_container">';
$list .= "\n\t" . '<h4 class="cateogry_letter">' . apply_filters( 'the_title', $letter ) . '</h2>';
$list .= "\n\t" . '<ul>';
foreach( $tags as $tag ) {
$url = get_tag_link( $tag->term_id );
$count = intval( $tag->count );
$name = apply_filters( 'the_title', $tag->name );
$list .= "\n\t\t" . '<li><a class="category_links" href="' . $url . '">' . $name . '</a></li>';
}
$list .= "\n\t" . '</ul></li>';
$list .= "\n\t" . '</div>';
}
}
} else $list .= "\n\t" . '<p>Sorry, but no tags were found</p>';
on my product pages I list the brands with
$tag_count = sizeof( get_the_terms( $post->ID, 'product_tag' ) );
?>
<div class="single_productbrand">
<?php echo $product->get_tags( ', ', '<span class="brand_as">' . _n( '', '', $tag_count, 'woocommerce' ) . ' ', '</span>' ); ?>
</div>
and it works as it should
How can I get the URLs back?
The main problem in your code related to the product tag terms links
is this line:
$url = get_tag_link( $tag->term_id );
It needs to be replaced with
get_term_link()
WordPress function that requires 2 arguments, the term object (or term ID) and the taxonomy.
As you might already now, Product Tags are a custom taxonomy 'product_tag'
…
Also in your code, even if it works, you should better have different variable names as you are using $tags
variable name in 2 different parts with different values.
So your revisited and functional code will be:
$taxonomy = 'product_tag';
$tags = get_terms( $taxonomy );
if( $tags && is_array( $tags ) ) {
foreach( $tags as $tag ) {
$first_letter = strtoupper( $tag->name[0] );
$groups[ $first_letter ][] = $tag;
}
if( !empty( $groups ) ) {
$list = '';
foreach( $groups as $letter => $letter_tags ) {
$list .= "\n\t" . '<div class="brand_category_container">';
$list .= "\n\t" . '<h4 class="cateogry_letter">' . apply_filters( 'the_title', $letter ) . '</h2>';
$list .= "\n\t" . '<ul>';
foreach( $letter_tags as $tag ) {
$term_link = get_term_link( $tag->term_id, $taxonomy );
$count = intval( $tag->count );
$name = apply_filters( 'the_title', $tag->name );
$list .= "\n\t\t" . '<li><a class="category_links" href="' . $term_link . '">' . $name . '</a></li>';
}
$list .= "\n\t" . '</ul></li>';
$list .= "\n\t" . '</div>';
}
}
}
else $list .= "\n\t" . '<p>Sorry, but no tags were found</p>';
// Output
echo $list;
This time the Urls are correct pointing to the right path...