Search code examples
csswordpresscodex

in wordpress, how do I add tags to the divs Class


I want to add the post tags into its div class so I can add specific CSS for different types of tags.


Solution

  • If you want to output the tag name(s) of the article being viewed into a div, do that :

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

    If you want to output the tag name that's being display (let's say the tag's page), do that :

    <div class="<? echo single_tag_title(); ?>">
    .....
    </div>