Different single post template per category wordpress

If you want to display single post page using different single post template, then wordpress in_category() function will help to create different single post template.

First thing you have to create different single post template like

single_portfolio.php
single_news.php
single_wordpress.php
single_default.php //For default single post template  for post other than above categories.

Above three files i create because i want to use different single post page template for my portfolio,news and wordpress category respectively.

You can simply create copy of your single.php file and rename it with new template and do the changes that you want for each category.

After that open your single.php file remove all code and add following code

<?php
  $post = $wp_query->post;
  if (in_category('portfolio')) {
      include(TEMPLATEPATH.'/single_portfolio.php');
  } elseif (in_category('news')) {
      include(TEMPLATEPATH.'/single_news.php');
  } elseif(in_category('wordpress')) {
      include(TEMPLATEPATH.'/single_wordpress.php');
  }
  else{
      include(TEMPLATEPATH.'/single_default.php');
  }
?>

8 Comments

  1. diozNo Gravatar
    May 21, 2010

    brilliant!

  2. Keith MountifieldNo Gravatar
    May 23, 2010

    Perfect! Great solution! Thanks for sharing.

  3. Tanveer MalikNo Gravatar
    May 24, 2010

    This is an excellent tutorial otherwise but am looking for a different variant of it. I am looking for a template which we can use to enter input of a new blog based on a template selected from the ‘Single Post Template’ drop down. The selected template will include input to custom fields as well. I would appreciate if anybody around can help me out.
    TIA

    • snileshNo Gravatar
      Sep 01, 2010

      Yes it can be done using the custom field.

  4. VenkateshNo Gravatar
    May 25, 2010

    I tried, but it’s not working… Definitely I’m missing something… Kindly anyone help me ?

    • snileshNo Gravatar
      Sep 01, 2010

      let me know what problem you are having

  5. WillNo Gravatar
    Jun 29, 2010

    What a brilliant solution, which beg the question, would it work if you use category ID instead? So we can format the single php template like the category-xx.php > e.g.: single-xxx.php?

    • snileshNo Gravatar
      Sep 01, 2010

      Yes there is no any specific reason for the .php files. i just used names to differentiate it.. you can use what ever names to the php files.. important thing is the in_category(‘category_namge’)

      you have to pass that category name.. and include respective php file.


Trackbacks and Pings

Leave a Reply