Am I Famous Now…

...I Am So Coordinated

Different Backgrounds for Categories in WP

January 6th, 2007 by WebMatrix · 4 Made Me Smarter

With only some additional lines of code it is easy to change the background of your WP entries for each category.
Actually you could even change the whole design for each category and use a diffferent header or sidebar for example. No limitations are set.

The only thing needed to do so is a conditional tag, and of course Wordpress offers that tag.

(in_category())

Using this condition and a classic if, elseif php call we can change everything we want for the categories we chose. The CSS, even the template.
You can use this in the header, using combined conditional tags to define if you want to use this site wide, or just for specific pages, categories or entries.

The background images (closing tags) and background color for several categories I use are easy to achieve.
Open in your theme folder the file index.php or if your theme uses a separate file for the loop, theloop.php and look for following line1

Original Cutline Code for Entries
This is where we have to insert our conditional tag and decide what we want to use to create a different background for entries in a category.

Changed Code For Cutline with Category Conditional Tag
As you can see I use a different class in the cascade for every category, but there are no limits for this principle2.
You could easily use this for a totally different entry template (the file single.php) for different categories, in which case your code would look like this

Different Entry Template for Different Categories
And obviously you can even call a totally different CSS for every categroy, or for specific pages. All based on the conditional WP tags.

1 This how-to is based on Cutline, and not Kubrick or k2. But the procedure is the same for anytheme, the classes might be different.
2 Do not forget to make the needed changes in your CSS!

Tags: , , , // 2,270 Views

4 have made me smarter ↓

  1. Gravatar

    1 Djizeus // Jan 6, 2007 at 10:02 pm// View all comments by Djizeus//  Add karma Subtract karma  +0

    That’s definitely a cool tool (and tip). Many blogs that I read would benefit of that.

  2. Gravatar

    2 Joefish // Jan 9, 2007 at 5:28 pm// View all comments by Joefish//  Add karma Subtract karma  +0

    Have you looked at the Sandbox theme? This has that functionality built-in. It moves the voodoo to functions.php to keep the loop clean. Er. Cleaner. Even a clean loop is still a little ugly. Anyway, it will set multiple classes for multiple categories and it uses category slugs instead of ids.

    It’s a pretty slick system, and it’s easy to replicate. I use an adapted version of this for my post classes.

  3. Gravatar

    3 Franky // Jan 9, 2007 at 5:48 pm// View all comments by Franky//  Add karma Subtract karma  +0

    I have the Sandbox installed locally, but never really went into the specifics of it.
    Problem for me with most of those adaptations I have is that many add-ons don’t interest me.

    BTW 1 : Saw you @ testers. :-P
    BTW 2 : I hope to manage it before July to move this blog to a new platform. Probably RoR.
    I will also follow the Habari evolution. I just like it slick. ;-)

  4. Gravatar

    4 Joefish // Jan 9, 2007 at 10:02 pm// View all comments by Joefish//  Add karma Subtract karma  +0

    I’m really interested in Habari. However, no PHP5 on my webhost yet. I’m definitely watching with interest.

Comment Policy

Spam comments are not supported and will be deleted.

This site supports a dofollow policy for comments, accordingly I do reserve the right to remove your URL if your comment seems to be nothing more than a plug to your site. This is a non-discutable decision from my side.

Stay polite and ad rem in the comments. We all, me too, have had our child days, but have grown up. Flame comments will be deleted.
If you really want to flame, feel free and do it on your own blog. Feel free to link back.

Off topic comments will be deleted.