WordPress.org

Forums

in_category adding a class in the ECHO (8 posts)

  1. CreativeWP
    Member
    Posted 3 years ago #

    I am working on this website, and I need to place some conditional codes. Basically, if some one is at the category called good_news, it should display in the <div class="category"> This is what I am doing now

    else
    if (in_category('good_news')) {echo ("background:#a5dce0 !important;");}

    This is the out-put:

    <div class="category" style="background:#a5dce0 !important;">

    How would I go to add an extra class inside the div?

    Also, I want to make the font color change: #000

    Thanks,

  2. If you are using the body_class() in your theme, you can easily do that.

    Please provide a link to your site and the category page.

    If you don't have the body_class() in your theme, you should add it. It's brilliant.

  3. alchymyth
    Forum Moderator
    Posted 3 years ago #

    please post the full code of the template into a http://pastebin.com/ and post the link to it here - see http://codex.wordpress.org/Forum_Welcome#Posting_Code

    if some one is at the category called good_news

    do you mean if someone views the category archive page of 'good_news' or if the post is in that category?

  4. Chip Bennett
    Theme Review Admin
    Posted 3 years ago #

    do you mean if someone views the category archive page of 'good_news' or if the post is in that category?

    The in_category() conditional applies to single blog posts that have the specified category.

    The conditional for the category archive index page for the specified category would be is_category().

    So, based on the OP, he wants the former case, i.e. a single blog post that has the specified category.

  5. Chip Bennett
    Theme Review Admin
    Posted 3 years ago #

    Actually, let me revise that; I see why you're asking for clarification. The OP uses in_category() in the code example, but describes the scenario as if some one is at the category called good_news<?em>.

  6. alchymyth
    Forum Moderator
    Posted 3 years ago #

    The in_category() conditional applies to single blog posts that have the specified category.

    The conditional for the category archive index page for the specified category would be is_category().

    you know that, and I know that.
    however, because the text does not match the code, I find it important to check.

    with a link to the problem site (which is missing as usual), this would be no issue...

  7. CreativeWP
    Member
    Posted 3 years ago #

    This is the link for the website I am working: http://bit.ly/ygSZdQ

    Basically, I want each category header to have its own background, and what ever else I may want within a class, font color, etc.

  8. alchymyth
    Forum Moderator
    Posted 3 years ago #

    it seem to be about the posts in the loop on the front page -

    try to change:

    <div class="category">

    to (remove your if(in_cat..) structure first):

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

    this will give you css classes such as:
    .cat-07_good_news

    (if a post is in more than one category, you will get several css classes.)

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

Topic Closed

This topic has been closed to new replies.

About this Topic