When I was creating this site, I had to figure out a way to display the single post page of my portfolio differently from a single page of a notebook entry. I wanted each to contain some different structure, and using one single.php page was not going to cut it. What if I wanted a different sidebar on the notebook page? After all, it’s content would not relate well with my front page sidebar.
So I looked for a more efficient and easy way to create multiple single page templates in WordPress. It’s actually rather simple. Open your single.php WordPress template and cut and paste the contents of it into a new file that you will name “single1.php”; this will be the template I use to display portfolio entries.
Then, paste the following:
[sourcecode language='php']
$post = $wp_query->post;
if ( in_category(‘notebook’) ) {
include(TEMPLATEPATH . ‘/notebooksingle.php’);
} else {
include(TEMPLATEPATH . ‘/single1.php’);
}
?>
[/sourcecode]
This special query is like a traffic router for your posts; if a post belongs in a certain category, send it to a certain template. If it’s not in any category, default back to the regular single.php template.
Querying posts and then filtering them by the category is smart and easy. In my case, if they belong to a certain category – in this case “notebook” – WordPress displays the notebooksingle.php template file. You can use as many separate templates as you want with the “else” property.
You can use as many different single page templates as you want, and easily separate them this way.
For instance, you could trigger as many templates as you like:
[sourcecode language="php"]
$post = $wp_query->post;
if ( in_category(‘5’) ) {
include(TEMPLATEPATH . ‘/article.php’);
} elseif ( in_category(‘6’) ) {
include(TEMPLATEPATH . ‘/column.php’);
} else {
include(TEMPLATEPATH . ‘/blogpost.php’);
}
?>
[/sourcecode]
I hope this helps someone.
No related posts.




Useful. One question. Where do I paste the new code:
post;
if ( in_category(‘5’) ) {
include(TEMPLATEPATH . ‘/article.php’);
} elseif ( in_category(‘6’) ) {
include(TEMPLATEPATH . ‘/column.php’);
} else {
include(TEMPLATEPATH . ‘/blogpost.php’);
}
?>
Please disregard my last comment.
I see now. I paste code in empty single.php
Wow, this is sweet and very easy to implement. Thanks Mike, this works perfectly for me.
Hi,
Can I do this with this system?:
Home —> shows every post
Page 1 —> shows every post of category A
Page 2 —> shows every post of category B
Page 3 —> shows every post of category C
Page 4 —> shows every post of category D
Best regards,
Thomas
@Brad, Daniel and Paul, thanks and I’m happy this was of some help.
Hi Thomas,
You can do that, but I think you’ll want to use a WP_Query to filter the posts the way you want ( by category a, b, c etc).
This is more about how to change the template for a single.php file; say for instance one of those category posts you want to look completely different.
Wow!!!
This code helped me solve a problem I was working on since a few days.
This article shoud be posted on de codex help-pages of WordPress.org.
Lot’s of people are struggling to list a certain categorie on a page different from the home-page!
Thanx alot!
Just wanted to drop by and say that it has helped me.
hehe.
I totally forgot about the `elseif` part to have multiple categories having a different single.posts.
I’ll gonna use this to exclude my aggregated stuff to be indexed by Search Engines.
OK I get what your saying haha nice work, but what if say I wanted to change the single depending on the page loaded. For example, I want a custom single.php for when register or profile.php is loaded, I a plugin to customize my login and profiles but need to find out how to load a custom single.php for the profile page mainly.I did try
at the begging of my single after the header is called but it doesn’t seem to work. Any ideas?
Thanks this is exactly what I have been looking for all day
You’re welcome Heather. Glad I could help a little.
Excelente! just what i was looking for, and now I think, how come i didn’t realize about this before!
Thanks Mike. This came in handy on a current project of mine. WordPress and the community surrounding it are really great for learning techniques like this!
Hello Mike,
This is the one I’m looking for. Thank you for sharing your script.
POSSIBLE?
================
include(TEMPLATEPATH . ‘/$CATEGORY_SLAG.php’);
Thanks a lot! I needed this for making my portfolio’ structure different than my blog’s strucutre. Thanks! I’m looking forward for your blog…
Thanks It was very useful
Hi Mike, that was very useful!
Do you know how to make this alternative page template use alternative comments and legacy comments php?
My site has 2 types of entries and I want to be able to manipulate comments differently.
Thanks for the great post!
Lana
Hi Lana
Glad you found it helpful. I’m not sure I follow what you’re trying to do with multiple comments, or conditional sorts of comments based on the type of template. I have not done something like that but if you have unique code snippets for each it should be a simple matter of inserting it in the proper place inside the loop.
Hi Guys,
I’m having a bit of a problem with this. I’m routing posts by category to two different single files; single-profile.php and single-minutes.php.
Problem is the first one works fine (so in theory the routing code in single.php is good) but the other returns a 404 error.
Any thoughts on where to look? The obvious one is that there is a problem in the single-minutes.php file but i’ve kept the code real basic and even stripped out the loop and just put in some static content to test but it always returns a 404!
cheers
Great! Save my day!
Thanks!
This doesn’t work for one reason: The one single pages cannot load the comments. It can show a form to add comments, but there is no way to load comments for that post. I am doing the same thing using my own code, and have been trying to find a solution for it. You didn’t find one did you? Another method I saw was simply swapping style sheet, but this won’t work for me because the websites are so different.
I haven’t seen this problem. What version WP are you using?
This is fantastic. I had to change it a little to get it working with my project in wordpress 3.0. I doubt the code will post, but:
post;
if ( in_category('blog') ) {
include(TEMPLATEPATH . '/blogsingle.php'); }
elseif ( in_category('events') ) {
include(TEMPLATEPATH . '/eventssingle.php');
}
else {
include(TEMPLATEPATH . '/single1.php');
} ?>
Glad it could be of some use to you Brian.
One more try, with added spaces:
post ;
if ( in_category('blog') ) {
include(TEMPLATEPATH . '/blogsingle.php'); }
elseif ( in_category('events') ) {
include(TEMPLATEPATH . '/eventssingle.php');
}
else {
include(TEMPLATEPATH . '/single1.php');
} ?>
doesn’t work in 3.0.1
the following works for me , i just copied into single.php.