WordPress.org

Support

Support » How-To and Troubleshooting » Category-based templates/styles

Category-based templates/styles

  • TechGnome

    @techgnome

    Moderator

    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

Viewing 15 replies - 1 through 15 (of 15 total)
  • 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.
    }
    ...

    sounds good to me

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

    LOL – rboren isn’t somebody … he’s a Dev 🙂

    Cool stuff this though!

    TechGnome

    @techgnome

    Moderator

    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

    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.

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

    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” />

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

    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?

    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

    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

    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.

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

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

    im still learning on it.

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘Category-based templates/styles’ is closed to new replies.
Skip to toolbar