Different Single Post Pages In Wordpress

July 8th, 2006, Published in Wordpress, 2,188 Views, Email This Email This

Wordpress

By default all Wordpress posts viewed as a single post are controlled by the particular theme's "single.php" file.

Wordpress, by default is setup to use one template file ("single.php") for every post.(But what if you wanted to display a different layout or different code dependent on the specific categories a post or filed into?

To do this with Wordpress; one needs to use a bit of PHP in the "single.php" file and create category specific files.

In the below example lets assume the following:

  • Category 2 is my Blog
  • Category 15 is a Photography section
  • Category 18 is a Video section

I want to display a different layout according to the particular post's category so I would edit "single.php" as follows:

<?php
$post = $wp_query->post;
if ( in_category('2') ) {
include(TEMPLATEPATH . '/single-blog.php'); } 
elseif ( in_category('15') ) {
include(TEMPLATEPATH . '/single-photography.php'); } 
elseif ( in_category('18') ) {
include(TEMPLATEPATH . '/single-video.php'); } 
else {
include(TEMPLATEPATH . '/single-default.php');
}
?>

Proceeding editing this, the referenced files must be created e.g. "single-blog.php", "single-photography.php" and "single-video.php" file referenced above. Also a "single-default.php" file should also be created so that any post that is not in any of the specified categories (15, 18 ir 2) will just use a default template file - "single-default.php".

<< Example Image Thumbnail Page Full Width and Height Images Uploads >>

5 comments so far

  1. Gravatar 1 Trevor Turk
    5:40 pm - 9-18-2006

    This is a great tip - I really like simple solutions - and this is a simple solution to a fairly complex problem.

  2. Gravatar 2 Alex
    6:43 am - 4-23-2007

    Thank You

  3. Gravatar 3 Lara
    2:14 am - 6-12-2007

    Thank you SOOOO much. This has saved my night from 12 hours of hair pulling trying to dissect PHP I can’t even understand.

  4. Gravatar 4 PJ
    2:25 am - 1-27-2008

    This was just what I needed too, thanks for sharing it.

  5. Gravatar 5 HERO
    11:52 pm - 4-22-2008

    Dude! I’ve looked all over for a solution like this! Thanks so much!

Leave a Reply

Your Details

Your Comment