WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Different CSS Based on Category (4 posts)

  1. noelgreen
    Member
    Posted 6 years ago #

    I'm wondering how to make a title of a post have different CSS based on what category it's in... or, better yet, if it's a sub-category or not.

    So, I have a post.
    Title: This Post
    Category: Main Cat

    This post would have an orange title using a serif font.

    Then I have a second post.
    Title: Second Post
    Category: Sub-Cat

    Sub-Cat is a sub-category of "Main Cat". "Main Cat" is its parent category in other words. So, "Second Post" would have a green title in a sans-serif font.

    Is this fairly easy to do?

  2. Adam Brown
    Member
    Posted 6 years ago #

    Sure. Suppose that your template wraps each post in something like this:

    <div class="post">
    ...
    </div>

    Try something like this:

    <div class="post <?php
    foreach((get_the_category()) as $cat) {
    echo $cat->cat_name . ' ';
    } ?>">
    ...
    </div>

    That will insert each category name as a class. Note that it must be within the loop.

    More info:

    http://codex.wordpress.org/Template_Tags/get_the_category

  3. Kafkaesqui

    Posted 6 years ago #

    You can also test for category with the in_category() tag:

    http://codex.wordpress.org/Template_Tags/in_category

  4. noelgreen
    Member
    Posted 6 years ago #

    Thanks guys!

Topic Closed

This topic has been closed to new replies.

About this Topic