Mike Meisner

I create attractive and functional websites

Posted by Mike
September - 28 - 2008

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.

Related posts:

  1. One category page, multiple headings Conditional PHP statements can be your best friend when developing...
  2. Deliver targeted ads to visitors using WordPress This is the Targeted Ads Approach Wouldn’t it be...
  3. WordPress SEO Trick – Optimize “Read More” Link I’m going to share a simple snippet of code that...
  4. 9 Essential WordPress SEO Plugins and Tips WordPress is the best platform to develop a site on...

Related posts brought to you by Yet Another Related Posts Plugin.

13 Responses to “Create multiple single page templates by category in Wordpress”

  1. Paul says:

    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’);
    }
    ?>

  2. Paul says:

    Please disregard my last comment.

    I see now. I paste code in empty single.php

  3. Daniel says:

    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

  4. Brad says:

    Wow, this is sweet and very easy to implement. Thanks Mike, this works perfectly for me.

  5. Thomas says:

    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

  6. admin says:

    @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.

  7. Thomas says:

    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!

  8. Kevin Paquet says:

    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.

  9. James says:

    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?

  10. Heather B says:

    Thanks this is exactly what I have been looking for all day

  11. Mike says:

    You’re welcome Heather. Glad I could help a little.

  12. Excelente! just what i was looking for, and now I think, how come i didn’t realize about this before!

  13. Mike says:

    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.

Leave a Reply

About Me

I am currently available for hire. If you need a basic website or a fully functional CMS for your business, I can help. I'm also pretty good with the whole marketing and SEO side of things and offer services in those areas as well.

Flickr

wineme2sonomasilverado