WordPress Single Post Templates

I’ve recently released a plugin that is far more robust than the methods outlined below. It actually duplicates the functionality of WordPress Page Templates, and allows you to create individual post templates, selectable on a per post basis via a dropdown menu. Head over to my WordPress Plugins page and find the “Post Templates Plugin”

Austin recommends using a filter in your functions.php file as an alternative to the method below. IMO, his suggestion is much simpler and quite elegant. Here’s the code to add to your theme’s functions.php file. (be sure you paste this code between <?php ?> tags):

add_filter('single_template', create_function('$t', 'foreach( (array) get_the_category() as $cat ) { if ( file_exists(TEMPLATEPATH . "/single-{$cat->term_id}.php") ) return TEMPLATEPATH . "/single-{$cat->term_id}.php"; } return $t;' ));

It helps solve the multiple categories issue since it cycles through all the categories in the array and checks to see which one of them has an associated post template. When it finds one, it uses the post template file, but if it doesn’t, then it falls back on the default single.php template.

So if you are used to placing posts in multiple categories, be sure to only create post templates for the categories you know won’t ever conflict.

Again, I highly recommend using this technique. It’s much simpler and works much better. If you care to, however, the original article is still below.

A few months ago, I wrote a post over at the Blog Herald explaining how to set up and use WordPress Page Templates to control the way indivudual WordPress “Pages” appeared on your blog.  The process was pretty simple … create the page template, insert the necessary code at the top of the file, and upload it to your themes folder.

What what about blog posts?  As far as I could tell, there’s no way to do a similar thing with single posts without some manual code (see the “Unique Single Template” section).

Is it possible to have post templates like page templates???

Cory sent me this simple question a few days ago, and it got me thinking. So I started investigating. [Read more...]

Comment Link in single.php

Ever noticed that the comments_popup_link function doesn’t actually output anything when viewing a single post?

I have … and until recently, I ended up just not including a link to the “comments” of a post when viewing a single post, and just hoping that either 1) no one noticed, or 2) no one would care, assuming they would just scroll down to the bottom of the post to see the number of comments.

But, I don’t like to assume. I like to think of the user as a person … a person that has normal expectations when using a theme.

Therefore, I decided to figure out a way (I’m sure I’m not the only one) to keep that darn link working … even when viewing a single post. [Read more...]