Elliot Jay Stocks is a designer, speaker, and author. He is the Creative Director of Adobe Typekit, the founder of typography magazine 8 Faces, one half of Viewport Industries, and an electronic musician.

Tutorial: Multiple single.php templates in WordPress

Posted on 09 July 2009 Comments

Article illustration for Tutorial: Multiple single.php templates in WordPress

WordPress has loads of cool features built-in for those of us who like to treat post categories differently, but sometimes a little extra hacking is required.

Take, for example, the category archives template, category.php. To have a different template for each category, all you have to do is add the category ID to the end of the filename; i.e: category-XX.php (where ‘XX’ is the ID of your category). That’s exactly how I achieved completely different-looking pages on the new elliotjaystocks.com: the Blog page uses a template called category-21.php, the Portfolio page uses a template called category-29.php, and the Speaking page uses a template called category-37.php. Simple, but extremely effective. *

So, with this in place, I started tackling the single.php template and assumed that you could do pretty much the same thing. I was wrong! WordPress has no built-in support for multiple single.php templates and so I had to find a way of doing this. I asked the Twitter community and got several responses, \* and my final solution works as follows:

  1. Delete everything in single.php
  2. Insert the ‘switching’ code (see below)
  3. Create two new templates with unique names
  4. On the server, the magical fairy dust in your modified single.php will automatically load the correct template when the page is requested

The ‘switching’ code

<?php
if (in_category('21')) {include (TEMPLATEPATH . '/single-21.php');
}
else { include (TEMPLATEPATH . '/single-29.php');
}
?>

Not much to it, is there? But that’s all you need. You’ll notice that I’ve followed the same naming structure as the category template variations, but you can actually call the files whatever you like. (I’d recommend this naming convention because it correlates to your category templates and it also makes things future-proof if WordPress ever decides to add native support for template-switching.)

And that’s it! Expect to see similar posts over the coming weeks, since there are a fair few WordPress hacks I’ve employed to get this site working the way it does.

* The posts in the ‘speaking’ category don’t need a single.php template since they’re never ‘clicked through to’; they appear on the speaking page in their entirety.

** If you want me to elaborate on this technique some more, please let me know in the comments.

\* I’m afraid I can’t remember who exactly came up with the final code I used; it may have been one person or several of you. I’m happy to give credit — my memory’s just crap. Give me a nudge if you think you deserve a mention.

© 2005 – 2014 Elliot Jay Stocks. All rights reserved. Powered by Siteleaf and tracked by Gaug.es. To keep updated with new content, you might like to subscribe to my RSS feed.