How to add class on term link?

I am looking for a way to add tag slug as a class.
I can’t figure out how to achieve this…

Here is how I display the products tags
<?php echo get_the_term_list( $post->id, 'product_tag'); ?>

Read More

The output is
<a href="http://myurl.com" rel="tag">My tag</a>

And I want
<a href="http://myurl.com" class="Tag_Slug" rel="tag">My tag</a>

Related posts

2 comments

  1. I think you should use a custom loop:

    <?php
    $terms = get_the_terms( $post->ID, 'product_tag' );
    if ($terms && ! is_wp_error($terms)): ?>
        <?php foreach($terms as $term): ?>
            <a href="<?php echo get_term_link( $term->slug, 'product_tag'); ?>" rel="tag" class="<?php echo $term->slug; ?>"><?php echo $term->name; ?></a>
        <?php endforeach; ?>
    <?php endif; ?>
    
  2. Why can’t you wrap the output in an unordered list something like below?

    echo '<ul class="styles">';
    echo get_the_term_list( $post->ID, 'styles', '<li>', ',</li><li>', '</li>' );
    echo '</ul>';
    

Comments are closed.