How to Set Up Custom WordPress Category Templates in Four Easy Steps

Oct 9, 2007

In a recent project, I needed to distinguish blog-style posts from longer, stand-alone articles in WordPress. The blog posts needed to look one way, and the articles, another. One type needed to look like a pretty typical blog post, but the other needed to look more like a New York Times story page. My basic goal was to categorize stories as either "posts" or "articles," and let WordPress apply the proper template for me. Seemed like it should be easy… until I tried to figure out how to make it work.

I found the explanations in the WordPress codex confusing and unhelpful. I searched around for some other solutions and didn’t find much to help me sort this out. Most articles or posts on the matter were knee-deep in code and long scripts. Finally, I found this post by Lorelle on "Creating Multiple Single Posts for Different Categories" which was tremendously useful. However, I still found the explanation a bit fuzzy. I sorted it out, but think a clearer take on this might help other similarly baffled WordPress users:

Step 1: Design two (or more) custom templates. A good way to start is to copy the "single.php" file from the default templete and modify it to suit your needs. In my case, I set up one template called "blogpost.php" and another called "article.php." I prefer to have descriptive template file names. Need more help? I’ve found Ben Gillbanks’ short tutorial on creating custom templates useful. Ok, so once you’ve got your custom templates worked up, copy them into your theme directory.

Step 2: Back-up your single.php file. Since the next step will destroy your single.php file as it currently exists, take a few seconds and make a copy of your working file, just in case something goes wrong. You can always replace your modified single.php file with the original to change everything back to the way it was before you started tinkering. Better safe than sorry, right?

Step 2: Turn the "single.php" into doorway to your custom templates. In a normal WordPress theme, the "single.php" file, also known as the "Single Post" template, takes a post and styles it with this basic template. But if you want to use multiple post templates, you can use this file to help WordPress understand which one to use. The WordPress engine looks at the single.php file to style your post’s content, but since it is a PHP file, you can use it to redirect it to your custom templates.

So what we’re going to do is replace the single.php file with some very short code that tells WordPress to check the category of a post and then load the appropriate template. You use the categoryID number from the Manage | Categories tab to identify the correct category. Here’s how the code would look:

<?php
$post = $wp_query->post;
if ( in_category(‘5’) ) {
include(TEMPLATEPATH . ‘/article.php’);

} else {
include(TEMPLATEPATH . ‘/blogpost.php’);
}
?>

So let’s break this down. the first two lines set up a query. The second line tells WordPress that if a post matches category 5 (which happens to be the ID for my "article" category… your number would vary, obviously), load the template "article.php". The next lines say, basically, "ok, so if this isn’t an article, it’s a blog post, so go ahead and load "blogpost.php".

But what if you have more than two category-based templates? No problem. Your new best friend is a little conditional tag called “elseif.” Here’s how that would work:

<?php
$post = $wp_query->post;
if ( in_category(‘5’) ) {
include(TEMPLATEPATH . ‘/article.php’);

} elseif ( in_category(‘6’) ) {
include(TEMPLATEPATH . ‘/column.php’);

} else {
include(TEMPLATEPATH . ‘/blogpost.php’);
}
?>

So this three-way query tells WordPress, "if a post is category 5 (an article), load the article.php template, if it’s not category 5, then check to see if it is category 6 (a column) and load the column.php template if it matches that, otherwise, it must be a post, so load the blogpost.php template." You can set up as many “elseif” statesments as you need to match the number of custom post templates you want to use.

Go ahead and save your single.php file. That short snippet of code is all you need. You’re almost done.

4. Give it a test. If you did everything right, the single.php should act like a traffic cop and direct posts to the correct template, depending on their category. Test posts by category and see how they display. If the templates seem broken or no post shows up, you may be missing a bracket or semicolon someplace in your single.php file. Go back and double check it. Conditional statements (if, else, and elseif) can be a bit confusing, so it can help to check the syntax here. You might also need to make sure your custom templates don’t have any odd code that might mess up the display.

Hope this helps. Drop me a line if you have questions or comments…

In a recent project, I needed to distinguish blog-style posts from longer, stand-alone articles in WordPress. The blog posts needed to look one way, and the articles, another. One type needed to look like a pretty typical blog post, but the other needed to look more like a New York Times story page. My basic goal was to categorize stories as either "posts" or "articles," and let WordPress apply the proper template for me. Seemed like it should be easy… until I tried to figure out how to make it work.

I found the explanations in the WordPress codex confusing and unhelpful. I searched around for some other solutions and didn’t find much to help me sort this out. Most articles or posts on the matter were knee-deep in code and long scripts. Finally, I found this post by Lorelle on "Creating Multiple Single Posts for Different Categories" which was tremendously useful. However, I still found the explanation a bit fuzzy. I sorted it out, but think a clearer take on this might help other similarly baffled WordPress users:

Step 1: Design two (or more) custom templates. A good way to start is to copy the "single.php" file from the default templete and modify it to suit your needs. In my case, I set up one template called "blogpost.php" and another called "article.php." I prefer to have descriptive template file names. Need more help? I’ve found Ben Gillbanks’ short tutorial on creating custom templates useful. Ok, so once you’ve got your custom templates worked up, copy them into your theme directory.

Step 2: Back-up your single.php file. Since the next step will destroy your single.php file as it currently exists, take a few seconds and make a copy of your working file, just in case something goes wrong. You can always replace your modified single.php file with the original to change everything back to the way it was before you started tinkering. Better safe than sorry, right?

Step 2: Turn the "single.php" into doorway to your custom templates. In a normal WordPress theme, the "single.php" file, also known as the "Single Post" template, takes a post and styles it with this basic template. But if you want to use multiple post templates, you can use this file to help WordPress understand which one to use. The WordPress engine looks at the single.php file to style your post’s content, but since it is a PHP file, you can use it to redirect it to your custom templates.

So what we’re going to do is replace the single.php file with some very short code that tells WordPress to check the category of a post and then load the appropriate template. You use the categoryID number from the Manage | Categories tab to identify the correct category. Here’s how the code would look:

<?php
$post = $wp_query->post;
if ( in_category(‘5’) ) {
include(TEMPLATEPATH . ‘/article.php’);

} else {
include(TEMPLATEPATH . ‘/blogpost.php’);
}
?>

So let’s break this down. the first two lines set up a query. The second line tells WordPress that if a post matches category 5 (which happens to be the ID for my "article" category… your number would vary, obviously), load the template "article.php". The next lines say, basically, "ok, so if this isn’t an article, it’s a blog post, so go ahead and load "blogpost.php".

But what if you have more than two category-based templates? No problem. Your new best friend is a little conditional tag called “elseif.” Here’s how that would work:

<?php
$post = $wp_query->post;
if ( in_category(‘5’) ) {
include(TEMPLATEPATH . ‘/article.php’);

} elseif ( in_category(‘6’) ) {
include(TEMPLATEPATH . ‘/column.php’);

} else {
include(TEMPLATEPATH . ‘/blogpost.php’);
}
?>

So this three-way query tells WordPress, "if a post is category 5 (an article), load the article.php template, if it’s not category 5, then check to see if it is category 6 (a column) and load the column.php template if it matches that, otherwise, it must be a post, so load the blogpost.php template." You can set up as many “elseif” statesments as you need to match the number of custom post templates you want to use.

Go ahead and save your single.php file. That short snippet of code is all you need. You’re almost done.

4. Give it a test. If you did everything right, the single.php should act like a traffic cop and direct posts to the correct template, depending on their category. Test posts by category and see how they display. If the templates seem broken or no post shows up, you may be missing a bracket or semicolon someplace in your single.php file. Go back and double check it. Conditional statements (if, else, and elseif) can be a bit confusing, so it can help to check the syntax here. You might also need to make sure your custom templates don’t have any odd code that might mess up the display.

Hope this helps. Drop me a line if you have questions or comments…

Like this article? Sign up for more!

Join our mailing list to receive articles on design, strategy, and building websites and email campaigns that get results.

Thanks! You've subscribed to Flying Dog articles.

Pin It on Pinterest

Share This