Support » Fixing WordPress » in_category adding a class in the ECHO

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

Viewing 7 replies - 1 through 7 (of 7 total)
  • Christine Rondeau

    (@crondeau)


    Volunteer Forum Moderator


    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.

    Michael

    (@alchymyth)


    Forum Moderator


    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?

    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.

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

    Michael

    (@alchymyth)


    Forum Moderator


    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…

    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.

    Michael

    (@alchymyth)


    Forum Moderator


    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

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘in_category adding a class in the ECHO’ is closed to new replies.