Forums

Changing Category CSS Based on Name (11 posts)

  1. dustinw
    Member
    Posted 1 year ago #

    Hi,

    I'm trying to change the CSS where the Category name is displayed, based on it's name. I thought this could be done with an IF statement, but I'm having problems...

    Basically, I thought it would be something like this:

    <?php if('category_name=Category1');?>
    <span class="Category1heading">
    <?php else:('category_name=Category2');?>
    <span class="Category2heading">
    <?php endif;?>
    </span>

    Could anyone give me a hand with this?

  2. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    <?php if(is_category('Category1')) $class=' class="Category1heading"';
    elseif(is_category('Category21')) $class=' class="Category2heading"';
    else $class='';?>
    <span<?php echo $class;?>><?php single_cat_title(); ?></span>
  3. dustinw
    Member
    Posted 1 year ago #

    Thank you for the reply! My limited knowledge looking at this, I thought it was correct. But, the Category isn't displayed, and I'm not getting any errors.

    I'm not sure why it isn't displaying anything... Any more ideas?

  4. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    Where are you using it? In what template and where?

  5. dustinw
    Member
    Posted 1 year ago #

    Sorry for the lack of details. I'm actually building a theme. It's on the index page. In the main loop where the posts are listed.

    Basically, I'm using the category titles as sort of a heading for different departments. My goal was to have the category's color/style to be a different color per department(category).

    Thank you again for your help.

  6. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    Ah - in that case single_cat_title() won't work (I assumed you were using this in a category template file). How are you grabbing the posts sorted by category?

  7. dustinw
    Member
    Posted 1 year ago #

    Currently, I'm not. In the main loop, I'm just listing all the posts, like normal. However, I'd like the category to be stylized based on it's department.

    For instance, there is an Engineering department. If a post is created within that category, I'm wanting the category's name to by a particular color at the top of that post.

    Hopefully that makes sense?

  8. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    What are you using to list the categories? the_category()? Only you can't insert category specific styling within that template tag. You'd need to "roll your own" category listing using get_the_category().

  9. dustinw
    Member
    Posted 1 year ago #

    I was using the_category().

    I see what you're saying now. So, I could probably use something similar to this:

    <?php
    foreach((get_the_category()) as $category) {
        echo '<span class="heading' . $category->cat_ID . '">' . $category->cat_name . '" />';
    }
    ?>
    </span>

    And then name each heading with the category id number at the end "heading1", etc.?

    Or am I misundertanding this?

    Thank you for all the help, and your patience.

  10. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    Yes - I think that solution is pretty much what you need.

  11. dustinw
    Member
    Posted 1 year ago #

    Thank you again esmi for your assistance. I wanted to post a solution to this, for anyone else that may run across this thread. There is actually a easy way to do this, based on a method described on this page:

    http://codex.wordpress.org/The_Loop

    It's about mid-way down the page, and conveniently titled "Style Posts From Some Category Differently". Apparently in my searches I just wasn't typing in the proper keywords to find this result before.

    Anyway, thanks again! Hopefully anyone else that needs this will find their solution here. :)

Topic Closed

This topic has been closed to new replies.

About this Topic