Displaying single post pages differently in specific categories
July 15, 2007 – 12:13 pm | by Miriam SchwabYou 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:
<?php
$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:
<?php
$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:
- 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.
- 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).
- 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 saysstyle.csstostyle-9.css. - 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.



15 Responses to “Displaying single post pages differently in specific categories”
By Ricky on Jul 16, 2007 | Reply
Hey, this would be great if it worked! Unfortunately I’m getting a 500 Internet Server Error. Not sure why. Any ideas?
By Senthil Kumar on Nov 6, 2007 | Reply
There is any specific plugin for this….
i am not aware of editing the code of my WP..
By Miriam on Nov 6, 2007 | Reply
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.
By Jack on Feb 3, 2008 | Reply
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!
By Paul on Feb 22, 2008 | Reply
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?
By Ryan on Feb 22, 2008 | Reply
Paul - yes that is possible via is_category. Here’s a demo of how it works:
<?php
if (is_category(cucumbers)) {echo ‘cucumbers’;}
elseif (is_category(tomatoes)) {echo ‘tomatoes’;}
else {echo ‘neither vegetable’;}
?>
By Ryan on Feb 22, 2008 | Reply
I forgot some quote marks:
<?php
if (is_category(’cucumbers’)) {echo ‘cucumbers’;}
elseif (is_category(’tomatoes’)) {echo ‘tomatoes’;}
else {echo ‘neither vegetable’;}
?>
By Miriam Schwab on Feb 22, 2008 | Reply
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.
By Paul on Feb 22, 2008 | Reply
Your the best!
I appreciate your n quick response and knowledge!
By Ryan on Feb 22, 2008 | Reply
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.
By Ryan on Feb 22, 2008 | Reply
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.
By Ryan on Feb 23, 2008 | Reply
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):
<?php
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:
<?php
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.
By Danish on Apr 22, 2008 | Reply
I’m going to try your tips. Thanks for share
Anyway is there any idea how to post in specific pages in wordpress?
Danish
Technology Mindset