Automatically assigning a customized single.php template to a post in a category

I have been using this plugin for years. Basically allows for the creation of post templates in the same fashion as is available via core to create page templates. So, just like with pages, all the editor need do to assign a (non-default) template to a given post is to make a check box selection … a great alternative to creating custom post types when the differences are just simple layout deviations.

But this approach does rely on human intervention, and is hardly automatic. There are times when you know ahead of time that every post from a given category should have a unique “look” so ….
There are articles that describe approaches bases on insertions into the functions.php file of the active theme like this one.

But after a while the functions.php file can get a bit overloaded, and so perhaps this is a better way to go …

Basically the idea is to create a series of new files in your theme based on the baseline single.php file like:

  • single_blog.php
  • single_portfolio.php
  • single_default.php

Then go to your baseline single.php file and remove everything (including the divs that wrap around the code – otherwise you will end up with weird wrapping behaviors that will drive you insane trying to find out where your CSS went wrong) inside the “content” area and replace it with something like this:

If you are using a “regular” theme, everything will be fine, however if you are using a child theme the code above will look for the alternate “single” files in the parent and throw errors all over the place. Now you could place the alternate “single” files in the parent to make it work, or convert your child theme to an independent theme and solve this problem … but that kind of defeats the whole beauty of the child theme process. Instead you need to “climb up” the tree out of the parent and send TEMPLATEPATH looking for the child theme like this:

Then everything works just fine!