rss

Using Different Templates for Posts & Pages

12 Jun, 2008 MarkB Single

Many times when using a WordPress site to manage static and dynamic content, you’ll have need to display posts in very different ways depending on the category they’re in. For example, if your site has products in various categories, as well as a blog news category, you’d most likely want to display your product listings and news listings in different ways. Here’s how you can do this.

The Template Method

Whenever WordPress needs to display ‘posts’, it uses the single.php template. What we’re going to do, is use the single.php file simply to redirect WP to the template it needs to use.

Firstly, remove all the content from your current single.php file and paste it in (for example) ‘template-blog.php‘. Then create a second file that you’ll use as your uniquely styled template, lets say ‘template-other.php‘.

Then add the following to your single.php file.

<?php
if (in_category('20')) {
   include(TEMPLATEPATH . '/template-other.php');
} else {
   include(TEMPLATEPATH . '/template-blog.php');
}
?>

The above code, is pretty simple to understand. You’re saying that if, the post being viewed is in category ‘20′, then use the template specified, otherwise revert to the other template ‘30′.

Simple!

You can also string multiple of such statements together using the elseif PHP command, like so:

<?php
if (in_category('20')) {
   include(TEMPLATEPATH . '/template-other.php');
} elseif(in_category('30')) {
   include(TEMPLATEPATH . '/template-unique.php'); 
} else {
   include(TEMPLATEPATH . '/template-blog.php');
}
?>

There’s another method that this can be done.

The Custom Field Method

I’m a big fan of WordPress custom fields and this is another great method you can use to easily assign templates to specific post categories.

What we’re going to say, is ‘if the custom field ‘wide’ exists, then use the specified template, otherwise use the other template. Here’s how it works:

<?php
if (get_post_meta($post->ID, 'wide', true)) {
   include(TEMPLATEPATH . '/template-other.php');
} else {
   include(TEMPLATEPATH . '/template-blog.php');
}
?>

Then when writing/saving your post, all you need to do is add a value of ‘yes’ to the template custom field that you’ll create called ‘wide’ and presto, your single.php file will spot the presence of the custom field and use the specified template.

You can of course also use both the above methods within your template if you want to, for example, add or remove certain areas from your page. You can do this by using if/else statements within your template. Like so:

Hopefully, this’ll give you some more ideas as to how you can further tweak your WP.

4 Comments

  1. Found this really useful, thanks!

  2. I’m glad, thanks Rob :)

  3. Excelent idea!
    I will be making good use of it.

  4. The customfield version is really what I was looking for. Thank you very much!

Leave a Reply

About The WordPress Guru

Over the last 4 or 5 years, I’ve been digging into the Wonders of WordPress. From the days when the Classic theme was the shizz until now, WordPress has gone from strength to strength both as the blogging platform of choice for many big players and as a CMS with...

Learn more »