Create Single Post Pages for Different Categories

Are you new here?

If this is your first time visiting our website, welcome! Learn more about us and what we do, or look through our complete blog archives for more good ideas.

Blog Posts from Addicott Web About Wordpress

Say you have a lot of categories and posts on your WordPress website, and want to have different single post pages for each category. How would you make this happen? It’s pretty simple actually – here’s how.

What you need to know

The way WordPress works, it tries to view each single post using your particular theme’s “single.php” file. Normally when you’re designing a WordPress theme, you would style that file however you want your posts to appear, and that would be the end of that.

If you want to have single posts display differently based on what category they’re in, it requires a slightly different approach. It’s very easy to do – anyone who has at least a basic knowledge of WordPress theme files should be able to do it.

Simply put, all you have to do is create a few additional files, add in some conditional code (which you can copy from below), upload to your WordPress theme directory, and that’s it – you’re good to go!

Step 1: Create the template pages

The first thing you want to do is create the different “single.php” pages for the posts in a particular category. The code in these should be what’s already used in your existing “single.php” file, although this is where you would style things a bit differently for each category by adding images, extra content, etc.

There are two things to keep in mind here:

  • Naming conventions – Name the different “single.php” pages in a way that makes it obvious to you which category they’re associated with – “single_categoryID.php“, “single_categoryNAME.php“, etc.
  • Create a default page – Call the file “single_default.php“, and then use the basic code in your template for how your posts should be displayed. This is a “fall back” page – you’ll see why in step 2.

Step 2:  Write the conditional code

What ties together the different single post pages that you just created in step 1 is some conditional code that tells WordPress what single post page to use. Put the following (and only the following) in your theme’s “single.php” file:

 <?php post;

if ( in_category('2') ) {
include(TEMPLATEPATH . '/single_category2.php'); }

elseif ( in_category('3') ) {
include(TEMPLATEPATH . '/single_category3.php'); }

elseif ( in_category('4') ) {
include(TEMPLATEPATH . '/single_category4.php'); }

else { include(TEMPLATEPATH . '/single_default.php'); } ?>

Basically, this code is telling WordPress to do two things:

  • Check the category – WordPress will check the post to see what category it’s under. If it’s in a particular category that you’ve included in the conditional code above,  WordPress will display the post using the single post page associated with that category.
  • Fall back to a default – When WordPress checks the category of a particular post, if it can’t find a single post page associated with that particular category (or if you haven’t created one), it will “fall back” and use the default single post page that you had created.

Note: you can keep adding conditions depending on the number of different single post pages that you need to create – you’re not limited to just three as in the example above.

With this principle, you can also…

The same ideas that we used above to create different single post pages can also be used to create different category pages.

Say you have 5 categories on your blog, and you want each category page to have a different introduction and image on it above the list of posts in the category. According to the WordPress codex page on category templates, all you have to do to make this happen is create separate pages for each category, and just name the files by the category ID – “category-2.php“, “category-3.php“, etc.

What’s happening behind the scenes is that WordPress’s template hierarchy is automatically checking to see if there are different pages used to display a particular category. If there are, it will use them, and if there aren’t, it will fall back to either the “category.php” page (assuming you have it in your theme) or the “archive.php” or “index.php” pages.

Thoughts?

Has anyone tried this method of using different single post pages on their WordPress website or blog? Or, have you just tried my method and have any questions about it (or want to share your success)? If so, share your thoughts with everyone by leaving a comment below!

Similar Posts

2 Comments

  1. Sumeet Chawla wrote on September 27, 2009:

    I needed to display different body headers for different category single posts. So, I was about to check for the categories inside the single.php file itself and display different body headers for different categories. But just for the sake of it, I googled for any better way and I landed here :) This method is much better just cause it lets you organize different templates for different single posts. :) Thanks ;)

    Posted at 1:44 pm

Trackbacks/Pingbacks

  1. Create Single Post Pages for Different Categories…

    Say you have a lot of categories and posts on your WordPress website, and want to have different single post pages for each category. How would you make this happen? It’s pretty simple actually – here’s how.
    ……

Leave a Comment