WordPress: Adding class selectors to the_tags(); output

How do I get the_tags() to output each tag so that it comes assigned with a unique class selector? So for example: the_tags() currently outputs something like this:

<a href="http://myblog.com/tag/kittens" rel="tag">kittens</a>

However, I’d like to output something like this:

Read More
<a href="http://myblog.com/tag/kittens" rel="tag" class="tag-kittens">kittens</a>

Is it possible to do this? If so, how? Thanks!

Related posts

Leave a Reply

4 comments

  1. It worked, thank you! This is what I did:

    <?php
    $post_tags = get_the_tags();
    if ($post_tags) {
      foreach($post_tags as $tag) {
        echo '<a href="'; echo bloginfo();
        echo '/?tag=' . $tag->slug . '" class="' . $tag->slug . '">' . $tag->name . '</a>';
      }
    }
    ?>
    
  2. Also you can overload working of get_the_tags(); function.
    Just add next code to your functions.php theme file:

    // add custom class to tag
    function add_class_the_tags($html){
        $postid = get_the_ID();
        $html = str_replace('<a','<a class="class-name"',$html);
        return $html;
    }
    add_filter('the_tags','add_class_the_tags');
    
  3. this code from http://www.lawturn.com

    /* SlSlib tags add class */
    <?php if(has_tag()) : ?>
    
        <?php
        $tags = get_the_tags(get_the_ID());
          foreach($tags as $tag){
            echo '<a href="'.get_tag_link($tag->term_id).'" rel="tag" class="tag-'.$tag->name.'">'.$tag->name.'</a>';
        } ?>
    
    <?php endif; ?>