Change CSS Styles with Different Categories in WordPress

Change CSS Styles with Different Categories in WordPress

Posted on 10. Feb, 2010 by Alex Denning in Theme Development

THE PROBLEM

Recently I needed to set up WordPress to change the background colour of the page with each parent category. After a bit of Googleing, it appeared no-one had an easy answer, so I turned to my good friend the if statement. I thought I’d combine an if statement with the WordPress template tag IS_CATEGORY. Soon turned out that that wasn’t an option; theis_category template tag only works for archive pages, not posts themselves.

THE SOLUTION

The solution was another template tag, IN_CATEGORY. Combine that with a number of different CSS files, each with a different colour, and I was left with the following:

<link rel="stylesheet" href="<?php bloginfo('template_url')?>/blue.css" type="text/css" media="screen,projection" />

<?php
if( in_category( 1 ) )
{
?>
<link rel="stylesheet" href="<?php bloginfo('template_url')?>/blue.css" type="text/css" media="screen" />	

<?php } elseif ( in_category (2) )
{
?>
<link rel="stylesheet" href="<?php bloginfo('template_url')?>/yellow.css" type="text/css" media="screen" />

<?php } elseif ( in_category (33) )
{
?>
<link rel="stylesheet" href="<?php bloginfo('template_url')?>/black.css" type="text/css" media="screen" />
<?php }	else { 	?>

<?php }	?>

And that’s it. Obviously, you’ll need to put the code in the header.php file and have these different stylesheets created, but other than that, done!

Related posts:

  1. Better iPhone WordPress Themes
  2. Really Easy Custom Post Backgrounds For WordPress

Follow on Twitter! Subscribe!
Alex's Gravatar

Alex Denning is the founder of WPShout. A WordPress developer from London, Alex co-founded WPShift at the start of 2010 where he sells awesome WordPress themes.

You can find Alex on Twitter and at AlexDenning.com.

4 Responses to “Change CSS Styles with Different Categories in WordPress”

  1. Devin

    10. Feb, 2010

    I do this with <a href="http://wordpresstheming.com/2009/12/dynamic-body-classes-wordpress-styling/"dynamic body classes, which means I can style on the basis of category + page, single, archive, date, tag, etc.

    Reply to this comment
  2. Devin

    10. Feb, 2010

    I do this with dynamic body classes, which means I can style on the basis of category + page, single, archive, date, tag, AND browser etc.

    I think your technique makes sense if there is a huge stylesheet with lots of differences- but if it something simple like just a background color change, styling with the body classes might be the way to go.

    Reply to this comment
    • Ashfame

      11. Feb, 2010

      Yeps! I totally agree with you, Devin.

      Reply to this comment
    • Alex Denning

      15. Feb, 2010

      Yep. They’re certainly the other way to do it; I had written half a post saying you can do it this way too, but scrapped it as customisation is limited.

      Reply to this comment

Leave a Reply

Please use your real name when commenting. Wrap code in <code> tags and make sure HTML is encoded. You can use regular HTML like <a href="... etc.

Get yours questions answered quicker

If you're using this post for paid work and have a question of any complexity then please ask at WPQuestions where you'll get a prompt response.