• Home
  • New Entries
  • Popular Entries
  • Submit a Story
  • About

WordPress Single Post Templates ...

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):

View More Info

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( isingle_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.
END UPDATE

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.
The Setup

You’re going to need 3 things:

   1. A basic knowledge of WordPress Themes
   2. A single.php file
   3. Multiple single templates, named according to category ID

The Code


What we’re going to do is create single post templates according to the ID of the category the post is filed under.  In order to determine what category a post is filed under, we’re going to use the get_the_category template tag.  Then we’re going to pull out the first category the post is filed under. Insert the following code at the TOP of your single.php file in your theme folder:

< ?php
$category = get_the_category();
$currentcat = $category[0]->cat_ID;
?>

Be aware, if you file posts under multiple categories, chances are this method will not work as you intended.

Then, we’ll add a little PHP magic to pull in the template file you created for a particular category:

< ?php
$category = get_the_category();
$currentcat = $category[0]->cat_ID;
if (file_exists(TEMPLATEPATH."/single-$currentcat".".php")) {
include(TEMPLATEPATH."/single-$currentcat".".php");
} else {
?>

This code defines the first category the post is filed under, checks to see if a single post template exists for that category (template files look like this: single-1.php, single-2.php, single-3.php, etc.), and uses it if it does.

Finally, we need to put the following code at the very bottom of the single.php file, normally just below the wp_footer function:

< ?php } ?>

The reason we have to do this is to be able to use the default single.php code as fallback in case you haven’t created a template for a certain category.

After you’ve got the code in place, just find the ID of the category you want to create a template for, then create a new file called single-ID.php (be sure to replace ID with the category number). Just insert the code you want to use for any posts filed in that category and you’re good to go!
Limitations

Like I mentioned earlier, if you are in the habit of filing posts under multiple categories, this code probably won’t work as intended. Since we’re working with templates for use on single posts in single categories, posts in multiple categories may end up falling back to the default single.php template. You have been warned!
Sample Code Download

As always, I like to provide a sample file for you to look at yourself. In this instance, I’ve edited the single.php file for the WordPress Default theme to pull in single post templates, if they exist. Take a look at the code if you have any questions about how it should look in your theme’s single.php file. I’ve also commented the code quite thoroughly.

source: nathanrice

 View Full Story.
Posted at 09:48:13 am | Permalink | Posted in Wordpress Themes  

Related Stuff

The WordPress Plugin That Hurt My Blog

I honestly didn’t think I’d figure out what exactly went wrong with my WordPress-powered blog that made this already invisible ...

WordPress Plugin Recommendations

This is a page that I’ll comtinually update as I do more work with WordPress Plugins. All I can guarantee is I’ll be ...

Speed up and clean up your WordPress

Every once in a while people will ask me to fix their blog, because it's either slow, or broken. Most of the time this is caused by ...

Simple Wordpress Digg Solution

I have looked around the interwebs to find the easiest way to add a digg button with the sumbit feature to ...

WordPress Theme Toolkit Plugin

If you ever edited WordPress theme with build in theme editor you probably know how uncomfortable it is. Default theme editor not only do ...

Be the first ... |Add your comment.

Your Comment ...

  Name (required)

  Email (required, hidden)

  Website


About Webloglines

Webloglines is a project offers a comprehensive collection of blogging services, articles, themes and plugins from around the world. Whether you're looking to promote your own blog or find blogs on various topics, this site is for you.


Recent Stuff

The WordPress Plugin That Hurt My Blog

WordPress Plugin Recommendations

Speed up and clean up your WordPress

Simple Wordpress Digg Solution

WordPress Theme Toolkit Plugin

WordPress SEO CMS


Our Partners

Ajax Projects

Web 2.0 Sites

Webloglines

Human Development Handbook

Software Development Company

Ajaxlines


Search


Topics

  • Blogging Tips (45)
  • Blogs Slides (25)
  • Blogs Websites (22)
  • Digg (5)
  • How to Blog (28)
  • Search Engines (2)
  • SEO (10)
  • WordPress Plugins (35)
  • WordPress Security (17)
  • Wordpress Themes (19)
  • Wordpress Tips (16)

© 2006 www.webloglines.com. All Rights Reserved. Powered by IRange