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.

No related posts.

26 Responses to “Create multiple single page templates by category in WordPress”

  1. Paul December 5, 2008 at 12:03 pm #

    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 December 5, 2008 at 12:24 pm #

    Please disregard my last comment.

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

  3. Brad January 6, 2009 at 3:54 pm #

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

  4. Thomas January 19, 2009 at 2:48 pm #

    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

  5. admin January 19, 2009 at 5:04 pm #

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

  6. Thomas January 19, 2009 at 7:10 pm #

    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!

  7. Kevin Paquet January 23, 2009 at 9:56 pm #

    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.

  8. James September 8, 2009 at 8:53 am #

    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?

  9. Heather B November 10, 2009 at 7:06 pm #

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

    • Mike November 10, 2009 at 8:52 pm #

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

  10. Gaston Suarez Duek March 11, 2010 at 10:07 am #

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

  11. Delwin March 24, 2010 at 12:06 pm #

    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!

  12. Jake March 28, 2010 at 2:46 am #

    Hello Mike,

    This is the one I’m looking for. Thank you for sharing your script.

  13. khen April 6, 2010 at 3:58 pm #

    POSSIBLE?
    ================

    include(TEMPLATEPATH . ‘/$CATEGORY_SLAG.php’);

  14. Noya Studio April 9, 2010 at 4:38 am #

    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…

  15. Sanjay July 8, 2010 at 6:09 am #

    Thanks It was very useful

  16. Lana Holy August 9, 2010 at 7:46 am #

    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

    • Mike August 9, 2010 at 11:37 am #

      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.

  17. Paul Williamson December 15, 2010 at 6:49 am #

    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

  18. Hugo December 17, 2010 at 12:59 pm #

    Great! Save my day! :D Thanks!

  19. Kayla April 23, 2011 at 12:36 am #

    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.

    • Mike April 23, 2011 at 1:07 am #

      I haven’t seen this problem. What version WP are you using?

  20. Bryan April 24, 2011 at 6:25 pm #

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

    • Mike May 5, 2011 at 9:21 pm #

      Glad it could be of some use to you Brian. :)

  21. Bryan April 24, 2011 at 6:27 pm #

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

  22. Zakir June 18, 2011 at 12:11 pm #

    doesn’t work in 3.0.1

    the following works for me , i just copied into single.php.