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.