WordPress.org

Ready to get started?Download WordPress

Forums

Category-based templates/styles (16 posts)

  1. TechGnome
    Moderator
    Posted 9 years ago #

    I was wondering, given a 1.5 install, and a customized theme, is it possible to load a different css file depending on the current category being displayed?

    I'm going to assume that this would be done in the header.php file, I could check for the currently displayed category, and if it is Cat X, Y, or Z, I can change the CSS link to X-layout.css, Y-layout.css or Z-layout.css. Does that sound reasonable? Can anyone find a flaw in that theory? Better idea?

    Tg

  2. Ryan Boren
    WordPress Dev
    Posted 9 years ago #

    That'll work. You can also have per category templates.

    If your theme supplies category-1.php and the category currently being quiered is 1, WP loads category-1.php instead of index.php. Your category-1.php could load a custom stylesheet. Add category-2.php, category-3.php, and so on. You can also use a sole category.php and code it like this:

    if (is_category(1)) {
    // Cat 1 stuff.
    } elseif (is_category(2)) {
    // Cat 2 stuff.
    }
    ...

  3. e-head
    Member
    Posted 9 years ago #

    sounds good to me

  4. Mark McKibben
    Member
    Posted 9 years ago #

    Sounds reasonable enough to me and if you replace:
    <style type="text/css" media="screen">
    @import url( <?php echo get_settings('siteurl'); ?>/fujiyama.css );
    </style>

    With something like this:
    <style type="text/css" media="screen">
    <?php
    switch($cat) {
    case "1":
    print "@import url(";
    echo get_settings('siteurl');
    print "/style1.css); n"; break;
    case "2":
    print "@import url(";
    echo get_settings('siteurl');
    print "/style2.css); n"; break;
    case "3":
    print "@import url(";
    echo get_settings('siteurl');
    print "/style3.css); n"; break;
    case "4":
    print "@import url(";
    echo get_settings('siteurl');
    print "/style4.css); n"; break;
    default:
    print "@import url(";
    echo get_settings('siteurl');
    print "/style.css); n"; break;
    }
    ?>
    </style>

    You should get what you're looking for.

    Edit: Drat, somebody beat me to replying. =)

  5. Mark (podz)
    Support Maven
    Posted 9 years ago #

    LOL - rboren isn't somebody ... he's a Dev :)

    Cool stuff this though!

  6. TechGnome
    Moderator
    Posted 9 years ago #

    rboren - will that work when viewing a post in one of those categories? Example, I have a particular look for my site as a whole. But, when posts from one of two categories in particular is viewed, I want a different look, not just for the category archives, which is cool, but also for the individual posts in those cats as well.

    Tg

  7. Flipjack
    Member
    Posted 9 years ago #

    I'm a big fan of this thread... your help may enable me to run my site as one blog, instead of having to do multiple installs simply for the sake of using different templates.

  8. Mark McKibben
    Member
    Posted 9 years ago #

    @TG: I'm not a dev, but I think Rboren's sample code should do what your asking. Though I'm not sure what his code will do if you use multiple categories on a single post.

  9. TyPierce
    Member
    Posted 9 years ago #

    Would it be possible to call different style sheets based on which category it is by coding in index.php?

    For instance, could I qualify the line:

    <link rel="stylesheet" href="<?php echo get_settings('siteurl'); ?>/wp-layout.css" type="text/css" media="screen" />

  10. westpointer
    Member
    Posted 9 years ago #

    I understand how this code could by used to get a different style sheet. Could it be modified to get an entirely different theme?

  11. westpointer
    Member
    Posted 9 years ago #

    That'll work. You can also have per category templates.

    How would you handle this if the permalinks are turning the cat into the friendly name?

  12. jberglund
    Member
    Posted 9 years ago #

    I'm also looking to do that same thing: Different Templates for each category, and have the same concern that Westpointer made: How would I handle this if I made my categories into permalinks? (which I did).

    Jberglund

  13. jberglund
    Member
    Posted 9 years ago #

    Ryan: Please forgive me, I'm new to PHP and am not completely sure how to properly 'code' the information you mentioned. I already tried to use Manabar's code... and I failed.

    Can you be more specific on how to type that and where I would need to put it?

    As I said... I'm still new to this, and am trying to learn it as I go. Any help would be GREATLY appreciated.
    Thanks!
    Jberglund

  14. nearlythere
    Member
    Posted 9 years ago #

    jberglund- maybe you've solved this already...

    i'm new to WP also, but it looks like you can still refer to the ID number of your category, whether you have made them permalinks or not.

    you also ask where the code should go, i assume you mean this code:
    http://wordpress.org/support/topic.php?id=20048#post-114255

    that would go in the head of your index file, replacing other links to style-sheets.

  15. jfunkk99
    Member
    Posted 9 years ago #

    I thought this was supposed to be a new "feature" in WP 1.5. Why isn't there any documentation on this?

  16. Rob Poitras
    Member
    Posted 9 years ago #

    there is, you just need to know what to do
    http://codex.wordpress.org/The_Loop

    im still learning on it.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.