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

  • can we include category names using comma? like 3,4,5? or we can use only multiple if statements? thanks
  • ISHAN SHARMA
    Best Tips to Secure Wordpress Blog/Site here http://how-what.blogspot.com/2...
  • kalen Johnson
    Great post, exactly what I was looking for. Works perfectly for me.
  • 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
  • Bhupenyadav25
    Thank...Its better description
  • Paul
    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!
  • Thanks for good trick!
  • Just what I was looking for. I already try and it works perfectly. Thank you!
  • Great, thanks! Just what I was looking for!
    Dave
  • 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
  • I have been wondering if those was possible for awhile. In my opinion, this single feature unlocks the full capabilities of WordPress. Amazing!
  • tom
    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?
  • Andy
    Exactly what I was looking for. Nice one.
  • Honey
    Excellent! Thanks so much for this - was exactly what I was looking for and worked perfectly.

    Legend.
  • Brilliant, worked perfectly! Thank you!
  • Adriana
    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!
  • Thanks! Got it working in min.
  • Ed
    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. :)
  • Thanks, this was just what I needed! Should of know it would be this simple!
  • Thanks, I'll try it ..
  • Thank you - this post was very helpful :)
  • Just what i was looking for and it works perfectly, thanx alot!
blog comments powered by Disqus