Different single post template per category wordpress

Home » Blog » 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');
  }
?>

Tags:

23 Comments on "Different single post template per category wordpress"

  1. dioz says:

    brilliant!

    Reply →
  2. Perfect! Great solution! Thanks for sharing.

    Reply →
  3. Tanveer Malik says:

    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

    Reply →
  4. Venkatesh says:

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

    Reply →
  5. Will says:

    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?

    Reply →
    • snilesh says:

      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.

      Reply →
  6. Walt Ribeiro says:

    This is exactly what I was looking for. Now my ‘songs’ and ‘blogroll’ category have different templates. Thanks!

    Reply →
  7. Christy says:

    Short and sweet. Just what I needed. Thanks!

    Reply →
  8. wow..thanks
    finally I found it here..

    Reply →
  9. antonio says:

    Very simple
    very functional

    Grazie mille

    Reply →
  10. That is BRILLIANT!!!! Thank you so much for your help that has made my life as a web designer much easier! That is exactly what I need in my web design!!!

    Reply →
  11. stacey says:

    I really like the posting … was easy to follow and very helpful while I was reading it … except, it’s not working for me.
    This is driving me nuts because it seems so simple but I’m having trouble.
    Because I’m using a child template, I had to change my code to:
    post;
    if (in_category(‘doggonecrazy’)) {
    include(‘http://crazydoglife.com/wp-content/themes/Nala-theme/single-cat-doggonecrazy.php‘);
    } elseif (in_category(‘humanrants’)) {
    include(‘http://crazydoglife.com/wp-content/themes/Nala-theme/single-cat-humanrants.php‘);
    } elseif(in_category(‘quote’)) {
    include(‘http://crazydoglife.com/wp-content/themes/Nala-theme/single-cat-quote.php‘);
    }
    else{
    include(‘http://crazydoglife.com/wp-content/themes/Nala-theme/single-cat-rainbow.php‘);
    }
    ?>

    This is actually getting the right template file to open because I’m getting

    Fatal error: Call to undefined function get_header() in /home/stacey/public_html/wp-content/themes/Nala-theme/single-cat-humanrants.php on line 10

    Why is it telling me that get_header() is undefined? I’ve been messing with it for hours and maybe it is obvious but I guess I’m missing the forest for the trees but I can’t seem to figure it out.

    If I replace single.php with it’s orginal code, including the call to get_header() it will go back to the way it was before.

    Any help would be apprectiated,
    Thanks

    Reply →
  12. randyyanuar says:

    so simple.. this is what i really need.. thanks a bunch dude… :)

    Reply →
  13. Mr.T says:

    I was messing around with filters and complicated stuff when I stumbled upon your post. Thank you.

    Reply →
  14. Jason says:

    Just what I was looking for. Thanks.

    Reply →
  15. Ericv says:

    That’s what I’m searching for. Great!

    Reply →
  16. kortita says:

    Oh My God!!!!
    Realy realy thank you for your share, i have been more than 72 hours without sleep for stupid this code, i founded many articles like this but all off sucks explanation and always following error, YOU ARE GOOD SIMPLE EXPLANATION for newbies like me,

    Good share sir!! God Bless you!!!

    Reply →
  17. Bart says:

    Great solution. I modified it slightly to fit my needs, as I have subcategories and I wanted to include them in my template (without having to update the code each time I added a new subcategory).

    Here’s my code:

    post;
    if (in_category( ‘cat1′ ) || post_is_in_descendant_category( get_term_by(‘slug’,'cat1′,’category’) )) {
    include(TEMPLATEPATH.’/single_cat1.php’);
    } elseif (in_category( ‘cat2′ ) || post_is_in_descendant_category( get_term_by(‘slug’,'cat2′,’category’) )) {
    include(TEMPLATEPATH.’/single_cat2.php’);
    } else {
    include(TEMPLATEPATH.’/single_default.php’);
    }
    ?>

    Reply →
  18. Bart says:

    I’ll try this once more. I forgot to include my code tags:

    <?php
      /**
       * Tests if any of a post's assigned categories are descendants of target categories
       *
       * @param int|array $cats The target categories. Integer ID or array of integer IDs
       * @param int|object $_post The post. Omit to test the current post in the Loop or main query
       * @return bool True if at least 1 of the post's categories is a descendant of any of the target categories
       * @see get_term_by() You can get a category by name or slug, then pass ID to this function
       * @uses get_term_children() Passes $cats
       * @uses in_category() Passes $_post (can be empty)
       * @version 2.7
       * @link http://codex.wordpress.org/Function_Reference/in_category#Testing_if_a_post_is_in_a_descendant_category
       */
      if ( ! function_exists( 'post_is_in_descendant_category' ) ) {
    	  function post_is_in_descendant_category( $cats, $_post = null ) {
    		  foreach ( (array) $cats as $cat ) {
    			  // get_term_children() accepts integer ID only
    			  $descendants = get_term_children( (int) $cat, 'category' );
    			  if ( $descendants && in_category( $descendants, $_post ) )
    				  return true;
    		  }
    		  return false;
    	  }
      }
    ?>
    
    <?php
    
      $post = $wp_query->post;
      if (in_category( 'cat1' ) || post_is_in_descendant_category( get_term_by('slug','cat1','category') )) {
          include(TEMPLATEPATH.'/single_cat1.php');
      } elseif (in_category( 'cat2' ) || post_is_in_descendant_category( get_term_by('slug','cat2','category') )) {
          include(TEMPLATEPATH.'/single_cat2.php');
      } else {
          include(TEMPLATEPATH.'/single_default.php');
      }
    ?>
    
    Reply →
  19. Lisa says:

    Thank you. Thankyouthankyouthankyou.

    Reply →

Got something to say? Go for it!

How to remove the http://t.co/XhUQ8T6p link from Login / Register Page: On self hosted wordpress blogs wordpress ... http://t.co/LWDOuEfq - 1 week ago