Support » Fixing WordPress » Using a category slug as a class in a template page

  • 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.

Viewing 1 replies (of 1 total)
  • Michael

    (@alchymyth)

    Forum Moderator

    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)

Viewing 1 replies (of 1 total)
  • The topic ‘Using a category slug as a class in a template page’ is closed to new replies.