Forums

Changing the class of a post based on a tag (2 posts)

  1. gkp99
    Member
    Posted 2 years ago #

    Hi,

    I'm new to WP theme development and PHP so please be gentle.

    I am writing a theme for my blog from scratch and the basics are in place (the posts show, there's a sidebar, header, etc).

    I make 3 different types of post: long, short and video. A post is tagged with either 'long', 'short' or 'video' as you might imagine to represent this.

    In the loop, when I display the posts they currently are in their own <div class="post">. What I want to do is style each type of post differently using CSS. To do this though, I need to make long posts <div class="post-long">, video posts <div class="post-video>, etc.

    How on Earth do I go about changing the class of a post based on it's tag? Is there an easier way of doing this or am I going about it all wrong?

    Thanks,

    Garry

  2. MichaelH
    Volunteer
    Posted 2 years ago #

    Could use something like this in your loop:

    <?php
    $tags = wp_get_post_tags($post->ID);
    if ($tags) {
    echo '<div class="post-'.$tags[0]->name . '">';
    }
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags