Create multiple single page templates by category in Wordpress
Skills: 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:
<?php
$post = $wp_query->post;
if ( in_category('notebook') ) {
include(TEMPLATEPATH . '/notebooksingle.php');
} else {
include(TEMPLATEPATH . '/single1.php');
}
?>
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:
<?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’);
}
?>
I hope this helps someone.











4 Comments
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.