Create multiple single page templates by category in Wordpress
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.

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
Hey I wanted to do the same as you in my css gallery, and your code actually save my ass! so, nothing more than THANKS A LOT!
Best regards from Uruguay!
Daniel
http://www.dubideas.com
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!
Glad it helped Gaston. You can really use this simple concept to include multiple instances of anything in your templates. It’s a bit rudimentary, and I am sure there are more elegant ways, for instance this guy has a better solution imo http://www.ericlightbody.com/2009/04/30/adding-multiple-sidebars-and-other-elements-to-your-wordpress-theme/ but either way, the simple PHP if/else commands can come in handy in a bunch of circumstances.
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.