Search code examples
phpwordpresscolorstags

Different color for tags in Wordpress?


So, I'm trying to give a certain tag a specific color in the front-end.

Let's say I have these two tags: private and business. I want the private tag to have a yellow color and the business tag to have a blue color.

I echo the tags with this code:

<?php
    $posttags = get_the_tags();
     if ($posttags) {
      foreach($posttags as $tag) {
       echo $tag->name . ' ';
      }
     }
   ?>

How can I give them the color I want, to be displayed at the front-end?


Solution

  • You may check for tag name and apply css to change the color, hope this helps

    $post_tags = get_the_tags();
        if ( $post_tags ) {
        foreach( $post_tags as $tag) :
            if ( $tag->name === 'private' ) :
        ?>
        <span style="color:#FFFF00;"><?php echo $tag->name; ?></span>
    
        <?php
            elseif ( $tag->name === 'business' ) :
        ?>
    
         <span style="color:#0000FF;"><?php echo $tag->name; ?></span>
    
        <?php
            else :
            // Post has neither tag, do nothing.
            endif; 
        endforeach;
    }