Different Category - Different Look: Creating Multiple Single Posts Looks for Different Categories
With the amazing help of the supportive folks on the WordPress Support Forum, my challenge was answered and I wanted to share this neat piece of template tag and conditional tag code with you.
My very popular series on CSS Experiments in Design consists of almost a dozen pages with hundreds of different design experiments. Most of these feature inline styles, but a lot of them had their own styles in a separate style sheet. The styles sheet was huge. With more than 600 articles on my site, why should I include over 20K of styles in my site's default style sheet when I only need them for a handful of articles?
I needed a way to let the style sheet for the CSS Experiment pages only appear on those pages and not the rest of the site. This is good for overall site optimization and faster access times.
With only one header template in my WordPress Theme, and the conditional tags saying "if this is a single page, show the single page", I needed something that said:
If this is a single page in the X category
show the single page with these styles added.
By default usage, the WordPress Template Hierarchy states that when you click a link to a single post page, WordPress will automatically look for the
What I wanted was to throw a condition in the single.php that says "if this post belongs to the X category, do something different." This is what we came up with.
Creating Three Single Post Templates
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.
This is just the start of what you could do. To showcase different results in different categories, you could create a long list of conditions like this:
<?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');
}
?>
In my two "single" copy template files, I put a comment code in the top of each one as a reminder of what each one was to do, like this:
<!-- single 2 - for CSS Web Page Articles -->
Since I don't want to change these two different single post templates, just add the additional style sheet to the second one, I created two header template files, exact copies like with the single.php, with an extra style sheet link in the top of the second one.
Inside of header2.php in the head section, I added the second style sheet link:
<style type="text/css" media="screen">
@import url('/wp-content/themes/mytheme/style.css');
@import url('/wp-content/themes/mytheme/cssstyles.css');
</style >
In the new single.2.php template file, I changed the GET for the header to get the header2.php:
<?php
/* Don't remove this line. */
require('./wp-blog-header.php');
include(get_template_directory() . '/header2.php');
?>
To test this, I uploaded all the files and clicked on any post NOT within category 9. Clicking View Source in the browser, I hunted for my comment tag and "single 1" or "single 2" in the code to see which "single" template was used. If it worked, I should see a comment that says:
<!-- single 1 - for all the rest of the pages -->
If I see "single 2" then something is wrong.
Then I clicked on a single post IN category 9 and did the same thing. There I should see the comment that this is indeed "single 2" and the two style sheet links should be in the header as proof that everything is done right.
There are many ways of doing this, as the PHP and conditional tags and template files used by WordPress are so versatile, but this was very easy to do for someone who is lacking in much PHP skill, though I'm learning the hard way. From this, you can make as many single post page looks as you want, as long as they are styled by their category.
Authors, photographers, teachers, and public speakers, Lorelle and Brent VanFossen, travel extensively with their camera and pen in hand to bring you a variety of articles on nature and travel photography including basic nature photography and the photography business, writing, travel, recreational vehicles, web page design, and life on the road. All images, design, and content are copyrighted and protected by law.
You can find related articles to this topic in our WordPress and Photography categories. The previous post is Big, Stark & Chunky: A List Apart and the next post is Separating Comments and Trackbacks and Pingbacks in Wordpress - The Answer. Different Category - Different Look: Creating Multiple Single Posts Looks for Different Categories, Issue Number 623, by Lorelle VanFossen, was updated February 26th, 2006.
You can follow comments through the RSS 2.0 feed and you can find more feeds on our Feeds List. Your comment is welcome, as are trackbacks from your site.
Related Articles
- WordPress Plugin - Just One Category
- Customizing the WordPress Loop for Excerpt Queries
- Fixing WordPress Category Separators
- Microsoft Free Program - Virtual WIFI
- Imitating MovableType Import File
- PhotoQuilts by Lorelle and Brent VanFossen
- Compromises Along the WordPress Import Path
- Really Rocking with WordPress
Submit Article: BlinkList | Blogmarks | Digg | Del.icio.us | Ekstreme Socializer | Feedmarker | Furl | Google Bookmarks | ma.gnolia | Netvouz | RawSugar | Reddit | Scuttle | Shadows | Simpy | Spurl | Technorati | Unalog | Wink | Yahoo MyWeb2


May 30, 2005
Thank you very much. This might be what I was looking for for so long!
July 22, 2005
wow this is great. that is obviously the solution for my nr.1 problem right now. thx alot. gretings from berlin
nicolas
July 22, 2005
… and by the way: you could do exactly the same with the archives.thats pretty cool. thx again
December 3, 2005
Hi
I like the technique but I cannot get it working with my wordpress setup.
I use permalinks, and as such links look like this: wordpressroot/category/post
I have applied the above code to my site. But I am getting errors. I am pretty sure I got the code right. But I think the problem is due to permalinks. Anyone got any ideas?
Example - http://www.alan-kay.com/category/computing
Click on any post in this section and errors apppear. Using:
post;
if ( in_category(’13′) ) {
include(TEMPLATEPATH . ‘/single-computing.php’);
elseif ( in_category(’4′) ) {
include(TEMPLATEPATH . ‘/single-film.php’);
} else {
include(TEMPLATEPATH . ‘/single-default.php’);
}
?>
December 5, 2005
Go through and check everything very carefully, and make sure that you are not using any characters in your quotes, just straight ascii quotes. And then check in with the WordPress Forum for more specific help as I’m on the road and will be unable to add more helpful information for a couple of weeks.
It takes one little character code mishap to screw everything up. Good luck with this.
August 8, 2006
Absolutely brilliant in its simplicity and stability. This is just what I needed. Thanx for sharing, I’ll get back to you with my next problem :)
October 5, 2007
Thanks! I was looking exactly for this for my website. You rock!
October 19, 2007
does it work with archive.php template? i have a monthly archive for a specific category and i want that when i click the monthly link, i want to display an archive page with the specific category only. thank you…
October 22, 2007
You might be able to get the conditional tag to work with the is_archive() filter, but I don’t know how to get it to only display a specific category. You will have to ask in the WordPress Support Forum for that kind of help.
Personally, I have found no usefulness is displaying date oriented content on a blog as few people, except the blogger, care when a post was published, they just want the information published to help them. Sorting any content by date, unless it is very recently, has been found to not be as efficient as searching for keywords, so I haven’t run into this. The forum may help.
October 8, 2008
I think the problem with the example that used elseif is that there were no closing curly brackets before each instance of “elseif” so copying and pasting it caused an error. It took me forever to figure out what the problem was since I’m a total newbie to php.