elegant themes

Displaying single post pages differently in specific categories

| July 15, 2007 | 33 Comments

You may have certain categories in your WordPress blog that you want to appear differently than other categories. For example, what if you have a category called “Tomatoes” and a category called “Cucumbers,” and you want the titles of every post in the Tomatoes category to be red, and the titles in the Cucumbers category to be green?

There are a few plugins that can help you with this, but they didn’t work so smoothly. The best solution I have found is one provided by Lorelle on her Taking Your Camera on the Road blog. In her post titled “Different Category – Different Look: Creating Multiple Single Posts Looks for Different Categories,” she gives the following directions:

We began by making two back up copies of the single.php page called single1.php and single.2.php.

Inside of the original single.php, delete everything and replace it with this:

$post = $wp_query->post;
if ( in_category('9') ) {
include(TEMPLATEPATH . '/single2.php');
} else {
include(TEMPLATEPATH . '/single1.php');

In the most simple terms, the PHP code issues a query that says “Check the post. If the post is in category ID number 9, display single2.php. If not in category ID number 9, display single1.php.”

In the in_category(), we set the category ID number to 9, the one that holds all of my web page design articles and experiments.

So basically, your main single.php file becomes a conditional file that tells WordPress which template file to use in certain categories. Lorelle also gives the following code you can put in the single.php file that will allow you to create multiple styles for multiple categories:

$post = $wp_query->post;
if ( in_category('9') ) {
include(TEMPLATEPATH . '/single9.php');
elseif ( in_category('12') ) {
include(TEMPLATEPATH . '/single12.php');
elseif ( in_category('42') ) {
include(TEMPLATEPATH . '/single42.php');
} else {
include(TEMPLATEPATH . '/single1.php');

Now you have to get the styles you want to apply to the correct category. Do the following for every category you want to have a certain style for:

  1. Create a new style sheet and call it style-9.css (9 is the category ID, but your category ID will likely differ. You can still call it style-9.css, but this is confusing!) If the changes you are making are minor, like changing certain colors or fonts, you should probably just copy style.css and rename it to style-9.css. In that style sheet, create the styles that you want, so for example if you want all h2 items to be red, make those changes in your new style sheet.
  2. Style sheets are called in your header.php file. Which means that we need to create a header file that will call your new style sheet. So copy your header.php and rename the copy header-9.php (or whatever you want).
  3. In your new header-9.php file, find the lines that look something like this:
    <link rel="stylesheet" href="<?php bloginfo('template_directory'); ?>/style.css" type="text/css" media="screen" />
    and change where it says style.css to style-9.css.
  4. Then, in your single2.php file, replace the following line (which is probably the first line):
    <?php get_header(); ?>
    with this
    <?php include ('header-hebrew.php'); ?>

Do this for every category. If you have a lot of categories you want to appear with different styles, there’s probably some conditional PHP that you can use. Being of little PHP knowledge, I don’t know what this code would be, but the above works quite nicely for people like me with little-to-no knowledge of PHP and only one or two categories that need modifying.

Tags: ,

Category: Tips

Comments (33)

Trackback URL | Comments RSS Feed

  1. […] Displaying single post pages differently in specific categories – ???? ????? ???? ???? ?????? ??? ????? ?? ????? ?????? ???? ?? ????? ?????? ????????? ????? ????? ????. ????? ??????. […]

  2. Ricky says:

    Hey, this would be great if it worked! Unfortunately I’m getting a 500 Internet Server Error. Not sure why. Any ideas?

  3. […] my previous post titled “Displaying single post pages differently in specific categories” for detailed instructions on styling single post pages for different categories. It’s all […]

  4. There is any specific plugin for this….
    i am not aware of editing the code of my WP..

  5. Miriam says:

    Hi Senthil – as far as I know, there isn’t a plugin that does this. If you want to achieve this, you might have to go into the code. But it’s not so bad.

  6. Jack says:

    Excellent! One of those things that is so simple you’re like “why didn’t I think of that”.. but I didn’t, so thanks!

  7. Paul says:

    Is there a way to change the sidebar advertising depending on what "vegetable" page you are on?
    Let’s say you have a blog that has those 125×125 ads – if you are on the Tomatos catagory page – the ads would be for afiliate ads for tomatoes. If you are on Cucumbers the ads would be replaced to show cucumbers ads. Home page would have it’s own set of ads.
    Any way to do this?

  8. Ryan says:

    Paul – yes that is possible via is_category. Here’s a demo of how it works:

    if (is_category(cucumbers)) {echo ‘cucumbers';}
    elseif (is_category(tomatoes)) {echo ‘tomatoes';}
    else {echo ‘neither vegetable';}

  9. Ryan says:

    I forgot some quote marks:
    if (is_category(‘cucumbers’)) {echo ‘cucumbers’;}
    elseif (is_category(‘tomatoes’)) {echo ‘tomatoes’;}
    else {echo ‘neither vegetable’;}

  10. Miriam Schwab says:

    Ryan, thanks for responding. I have a question – where it says "echo ‘cucumbers’", what is it calling? Is it calling the cucumber sidebar or the cucumber template, or what?

    As you can see, my php is very poor.

  11. Paul says:

    Your the best!

    I appreciate your n quick response and knowledge!

  12. Ryan says:

    No, the echo ‘cucumber’ doesn’t call anything. An echo statement just outputs HTML to the page. So in that example, the word cucumber would be displayed when you were on the cucumber category page (note: this doesn’t work for single posts within that category, just the category page itself).

    To display an advertisement or similar, you would just replace the word cucumber inside the echo statement with the HTML you want displayed.

  13. Ryan says:

    Also, you can replace the is_category with a bunch of other options, including is_page for WP pages, is_single for single posts, is_archive … well you get the idea.

    There’s more information on the conditional tags page on WordPress.org … http://codex.wordpress.org/Conditional_Tags

    These ‘tags’ as WordPress calls them are particularly useful for keeping your theme files to a minimum. Many themes repeat their code over and over which seems kind of pointless when a quick single line of code can replace an entire file a lot of the time.

  14. Ryan says:

    I haven’t tested the following to see if it works, but I thought I would share it in case it is of use to any of you.

    Another way to style your category pages, rather than the method Miriam has suggested above, is to use the conditional comments directly inside your header.php file, AFAIK this should behave the same way as in single.php.

    The following is all that would need to be added to the header.php file (between the HEAD tags):
    if (is_single()) {
      $post = $wp_query->post;
      if (in_category(‘cucumber’)) {
        echo ‘<link type="text/css" rel="cucumber.css" />'; 

    You could also apply it to the category page itself too with the following code:
    if (is_category(‘cucumber’)) {echo ‘<link type="text/css" rel="cucumber.css" />'; }

    if (is_single()) {
      $post = $wp_query->post;
      if (in_category(‘cucumber’)) {
        echo ‘<link type="text/css" rel="cucumber.css" />'; 

    Like I said though, I haven’t tried it to see if it works. There may be errors in the code above.

  15. Danish says:

    I’m going to try your tips. Thanks for share

    Anyway is there any idea how to post in specific pages in wordpress?

    Technology Mindset

  16. Jack says:

    Using multiple styles for different categories has been getting my brain in a mess lately, but this solution is perfect! Can’t get much simpler either, thank you for sharing!

  17. JamieO says:

    <a href=”http://idealienstudios.com/code/ICE/”>Idealien Category Enhancements</a> provides this functionality through a plugin. You can setup post templates by category. More importantly, you can create the templates in your theme with a useful name (categoryTemplate-Photos.php or categoryTemplate-Blog.php) and select which gets applied based on the Manage > categories admin panel. Same for postTemplate samples.

  18. Noryt says:

    Hey Mirria,

    Very cool tip, just what I was looking for. Thought about using multiple conditional loops, but this was certainly the coolest way out. Btw, your website has saved on a couple of occassions. Just thought now would be a good time to write down a thankyou.

  19. Miriam Schwab says:

    @Noryt: Thank yous like this one are what helps me keep going! (Although as you can see my posting frequency has gone down dramatically :( It’s hard to keep it up and get work done AND have a life. Oh well.)

  20. Chris Burns says:

    This article saved my butt so I just thought I’d let everyone know that if you’re trying to use more than 2 single.php files the above code is slightly off and will not work as written.  To fix it you simply have to insert a “}” – that’ a closing curly bracket before each instance of “elseif”

  21. If it weren’t for the comment from Chris Burns I would’ve given up on this – but it works perfectly, thank you very much indeed!

  22. Josh says:

    It’s a good read, but i am guessing there is a more simple method out there and your article got me thinking…
    What it you write a php code that says: if the category is ‘X’ then get stylesheet “X.CSS”?
    I think that’s a far more simple method but the thing is i know too little about PHP but i think it is possible.
    I need to change my title colors and that’s i was reading this. If nothing esle crops up then i’ll need to use your solution.  :P

  23. Nick Soper says:

    A super simple way to do this is simply create a new single.php file called single-9.php or single-2.php, the number being the category number of the custom page you want.
    (Note this works with category.php too eg. category-8.php)
    As this is built into wordpress, performance should be a bit quicker than an if statement in the template.

  24. Domonic says:

    I thought i got it right up until you mentioned ‘header-hebrew’ thats gotten me confused…Obviously you mean somoething else, can you explain please?

    • @Domonic That’s just the name of the header file we were using in this example since we wanted a certain category to display a Hebrew header. It could have been named header-gibberish or anything else. So just replace the name of the header file with whatever you think you’ll name your header file.

  25. […] article : tips/displaying-single-post-pages-differently-in-specific-categories goes over some great methods for using the default Single.php file as a way of determining which […]

  26. Tommy says:

    The method can be done with categories, tags, and pages. However I don’t think it can be done with single yet.
    Here is a link to the WordPress Codex which further details hierarchy. Notice with single there is no slug or post # available:

  27. Tommy, that’s exactly what this tutorial teaches you to do: style single post pages differently according to which category they are in. Yes, you can also style complete categories/tags etc. a certain way, but this allows you to style single pages in those categories too.

  28. Steve says:

    Just wanted to say thank you for this post, even though it is slightly old. This helped with getting my PHP to display the comments template ONLY for pages that are within a certain category. Worked like a charm!

    $post = $wp_query->post;
    if ( in_category(’16’) )
    else {

  29. seanjacob says:

    The second code example is wrong, closing } brackets need to be inserted.

  30. Bruce C says:

    Good tutorial. I have multiple categories on LatestBlogPosts.com and this came in handy as different categories of blogs display differently.


  31. Bruce C says:

    Hey, I just tried this after my last comment and this works really well, but I did not use the includes. Instead, I just modified my original single.php with the in_category() function as you specified above.

    Obviously, it works either way. Thanks. I also needed this for an ecommerce website.


  32. Kevin says:

    header-hebrew.php? Do you mean to call the new header file that calls the new stylesheet? (in your example, header-9.php). i don’t have a header-hebrew..

Leave a Reply

If you want a picture to show with your comment, go get a Gravatar.

More in Tips (67 of 96 articles)