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

  1. chrisjb
    Posted 6 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


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

    Many thanks in advance.

  2. Michael
    Forum Moderator
    Posted 6 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

    (thanks to @vtxyzzy for the suggestion)

Topic Closed

This topic has been closed to new replies.

About this Topic