WordPress.org

Ready to get started?Download WordPress

Forums

Using a category slug as a class in a template page (2 posts)

  1. chrisjb
    Member
    Posted 4 years ago #

    I'm having some real trouble finding the solution to this.

    I'd like to make the class of posts displayed on my home page defined by the category slug of the category they belong to.

    Like so:

    <div class="category-slug">

    Post content

    </div>

    I cannot find the solution to this anywhere. Can anyone please direct me?

    Many thanks in advance.

  2. alchymyth
    The Sweeper & Moderator
    Posted 4 years ago #

    assuming there is only one category for the post:

    <?php $categories = get_the_category();
           $cat_slug = $categories[0]->slug;
           echo "<div class='$cat_slug'>";   ?>
           Post content
           </div>

    (thanks to @vtxyzzy for the suggestion)

Topic Closed

This topic has been closed to new replies.

About this Topic