WordPress.org

Ready to get started?Download WordPress

Forums

CSS and catagories (5 posts)

  1. orbital318
    Member
    Posted 5 years ago #

    I am building a site where i need it to be sensitive to one category and load a special CSS just for that section. The CSS should over right the main CSS. Any info on this would be a wonderful help

    thanks! Rob

  2. SteveSmith1983
    Member
    Posted 5 years ago #

    On my site I add the categories to the post div classes:

    <div class="post<?php
        // add classes for categories
        foreach ((get_the_category()) as $category) {
            echo ' ' . $category->category_nicename;
        }
        ?>" id="post-<?php the_ID(); ?>">

    category-nicename gives the category slugs, so by default a 'review pages' category would be 'review-pages', for example.

    Say you have a category called 'reviews', make sure .reviews { } is below everything else in the CSS, so it overrides the settings above.

    This won't replace the CSS of standard posts, but you can reset it all, e.g. if posts have margin: 1em then do margin: 0 to get rid of it in .reviews.

    If you have a separate CSS file, you could use @import at the bottom of style.css to add it on. If you don't want to add the category classes to the separate CSS file, you could use some PHP to do the @import only on the pages with that category.

  3. @mercime
    Volunteer Moderator
    Posted 5 years ago #

    Nice one Steve, cool.

  4. SteveSmith1983
    Member
    Posted 5 years ago #

    :)

  5. danapalooza
    Member
    Posted 5 years ago #

    Cool, indeed. Flippin' huzzah, is what I say.

    I've been using AsideShop for some time now and have recently begun to wish I could do more with it. It was a blessing but it also tended to get in the way in certain situations. As much functionality as it gave, it also could take some away.

    This solution works great so far. Thanks for the PHP skillz.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags