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“.

37 Comments Add Your Own Comment

(October 29th, 2008 at 4:36 am )


Just what i was looking for and it works perfectly, thanx alot!

(December 6th, 2008 at 12:12 am )


Thank you – this post was very helpful :)

(February 8th, 2009 at 12:13 am )


Thanks, I’ll try it ..

(February 14th, 2009 at 5:06 am )


Thanks, this was just what I needed! Should of know it would be this simple!

(February 22nd, 2009 at 3:00 am )


Hi WordPress Hero,

How could you do the same based on post-id instead of categories?

That way you have absolute freedom, the opportunity to use multiple custom single post templates in a single category. :)

(March 6th, 2009 at 4:38 pm )


Thanks! Got it working in min.

(March 8th, 2009 at 11:27 am )


THANK YOU! I tried a number of tutorials, none worked. This was the simplest one and presto. Thank you for taking the time to share this!

Indeed you are a WP hero!

(March 25th, 2009 at 8:36 pm )


Brilliant, worked perfectly! Thank you!

(April 29th, 2009 at 5:16 pm )


Excellent! Thanks so much for this – was exactly what I was looking for and worked perfectly.

Legend.

(May 1st, 2009 at 8:54 am )


Exactly what I was looking for. Nice one.

(May 9th, 2009 at 10:38 pm )


Hi,
this looks very interesting. But what if you have a post which is related to category 2 and 18?

What will happen with posts which are in more than one categories with different templates?

(June 14th, 2009 at 6:56 pm )


I have been wondering if those was possible for awhile. In my opinion, this single feature unlocks the full capabilities of WordPress. Amazing!

(July 1st, 2009 at 10:32 pm )


Thanks!
This was/is a great solution for this issue! My only suggestion would be to add this to the Codex as I suspect plenty of others would also love to find this as well.
Best,
David

(July 10th, 2009 at 4:12 pm )


Great, thanks! Just what I was looking for!
Dave

(August 20th, 2009 at 1:09 am )


Just what I was looking for. I already try and it works perfectly. Thank you!

(August 31st, 2009 at 11:25 am )


Thanks for good trick!

(April 6th, 2010 at 9:46 am )


WORKS PERFECTLY!!
It’s exactly what I needed. MANY thanks!!!

(August 2nd, 2010 at 3:49 pm )


Is there any way to link from one single page template to the other?

i.e. I want my home-page slideshow to link to single-1.php to show larger versions of the project then I would like to have a link from there to the full article in single-2.php. Using this method loops back to single-1.php when they click on the readmore link. Is there a way to get around this?

Thanks!

(July 11th, 2011 at 9:20 am )


Thank…Its better description

(October 19th, 2011 at 4:00 pm )


Hi, thank you. it is really great tutorial but can I change the
if ( in_category to if ( in_tag ? Sorry, I'm newbie here, but I really interesting with your tutorial, thank you

(December 8th, 2011 at 3:43 pm )


Great post, exactly what I was looking for. Works perfectly for me.

(December 18th, 2011 at 12:37 pm )


Best Tips to Secure WordPress Blog/Site here http://how-what.blogspot.com/2

(January 15th, 2012 at 5:21 pm )


can we include category names using comma? like 3,4,5? or we can use only multiple if statements? thanks

(March 2nd, 2012 at 12:02 pm )


I was looking for this only.. Its nice put up.. Excellent trick.. Now I can Implement Different Templates for Different Posts Category in my site 
http://www.c4learn.com/ thank you very much again..

(March 30th, 2012 at 6:21 pm )


Thank  you for this post. Works perfectly :)

(May 2nd, 2012 at 10:51 am )


Very technical and nice post..I was just passing through and find your post..I learn few things from it.Thanks for sharing.

(May 25th, 2012 at 3:42 pm )


Greeeaaaaat !!! I'm going to try right now, that's exactly what i'm looking for :). Thank you for this post !

(December 21st, 2012 at 6:49 am )


thaks , this is what i want to do.its very helpful for me.thanks a lot

(April 29th, 2013 at 6:11 pm )


how i can find the category if iam using Custom Post type ..?

(December 26th, 2013 at 5:34 am )


Thank you so much :)

(March 5th, 2014 at 10:12 am )


wonderful.. it has helped me a lot.. works 100% thank you

(July 30th, 2014 at 2:39 am )


When using a child-theme, use STYLESHEETPATH in stead of TEMPLATEPATH… :)

(September 7th, 2014 at 2:11 am )


Good idea, Thank you.

Add Your Own Comment Fields marked * are required.