Different Stylesheet For Different WordPress Categories

Here’s an interesting question I found on Twitter today. Here’s what she asked.

Anyone know how you over write they style sheets for one page on a WordPress blog?

hexcodessm
Photo credit: Diego Sinning

I’m not sure if she’s referring to Pages in WordPress or categories. In both cases it is possible but you have to be prepared to write a small php command. Here’s where knowing just a little bit of basic PHP can go a long way when working with WordPress.

There are several methods of doing this but we’ll only cover one here to keep things simple. Let’s say we want a special style sheet just for the home page. This is what you’d put in your header.php of your theme. Find the line of code that references the style sheet. It would look something like this.


Change that out to something like this

< ?php if (is_home()) { ?> 

< ?php } else {?>

 < ?php } ?>

Make sure you enter the correct name for the alternate style sheet. In this case, I’ve named my alternate style sheet style2.css

You can also specify a different style sheet for different categories, authors, specific Pages or single post page by using different conditional tags as explained in the Codex.

Example if you want the style2.css for category 2 archive page, just switch out is_home() for is_category(‘2’) where the number 2 is the category ID.

Do You Want A Hands-Free Business?

Then get this guide to help you systemize your business so you'll have more time working on your business.

!
!

Hey! I want to make sure you know what you're getting here. In addition to the guide, you will also receive our memo that includes special offers, announcements and of course actionable information.

Terms and Conditions checkbox is required.
Something went wrong. Please check your entries and try again.
Facebook Comments

8 Comments

  1. Parody on October 26, 2008 at 1:20 pm

    Great post, its exactly what im looking for! But how would you do multiple conditions ? As in one for home one for archives, one for pages etc ??

    Ive tired the above and it does not work ! Im sure its something simple but i just cant get my head around it.

    Thanks for the great post.



  2. Parody on October 26, 2008 at 9:20 am

    Great post, its exactly what im looking for! But how would you do multiple conditions ? As in one for home one for archives, one for pages etc ??

    Ive tired the above and it does not work ! Im sure its something simple but i just cant get my head around it.

    Thanks for the great post.



  3. Lynette on October 27, 2008 at 4:26 pm

    Hi Parody. You’ll need to do an if else statement. Like this:

      <link rel=”stylesheet” type=”text/css” href=”/style2.css” />
      
      <link rel=”stylesheet” type=”text/css” href=”/style3.css” />
      
      <link rel=”stylesheet” type=”text/css” href=”” />
      

    Check out WP’s Conditional Tags page in the Codex. While it does not explain it for style sheets, the concept is the same.
    http://codex.wordpress.org/Conditional_Tags



  4. Lynette on October 27, 2008 at 12:26 pm

    Hi Parody. You’ll need to do an if else statement. Like this:

    
      /style2.css” />
      
      /style3.css” />
      
      ” />
      

    Check out WP’s Conditional Tags page in the Codex. While it does not explain it for style sheets, the concept is the same.
    http://codex.wordpress.org/Conditional_Tags



  5. Parody on November 24, 2008 at 4:01 am

    Lynette !! your champion!

    Thank you so much ! I’m using it now on my site.



  6. Parody on November 24, 2008 at 12:01 am

    Lynette !! your champion!

    Thank you so much ! I’m using it now on my site.



  7. Lynette on November 24, 2008 at 3:48 pm

    Hello again Parody, you’re most welcome.



  8. Lynette on November 24, 2008 at 11:48 am

    Hello again Parody, you’re most welcome.