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'); ?>

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>

2 Answers
2

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="https://wordpress.stackexchange.com/questions/130622/<?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; ?>

Leave a Comment