Elliot Jay Stocks is a designer, writer, speaker, and author, currently serving as the Creative Director of ClearScore, and the Co-editor and Creative Director of lifestyle magazine Lagom. Previously, he was the Creative Director of coffee roaster Colonna, the founder of typography magazine-turned-book 8 Faces, and the Creative Director of Typekit (now Adobe Fonts). He’s also an electronic musician, recording as Other Form and releasing on the Berlin-based label Unterwegs.

Tutorial: Multiple single.php templates in WordPress

Posted on 09 July 2009

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

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 – 2019 Elliot Jay Stocks. All rights reserved. Proudly powered by Kirby. To keep updated with new content, you might like to subscribe to my RSS feed.