123 Responses

  1. Landing Page Templates
    Landing Page Templates Published |

    Thanks for explaining the template method. I want to create one.

  2. Jean-Baptiste Jung
    Jean-Baptiste Jung Published |

    Excellent tutorial. Thanks for sharing your knowledges, Justin!

  3. Lyndi
    Lyndi Published |

    A tricky topic well explained. Very nice tutorial.

  4. J Mehmett
    J Mehmett Published |

    This is so nice. Though I have no clue of why I would need such template, it seems that large news oriented sites may need it for something about BIG event or once-in-a-lifetime article which needs special attention.

    This is part of WordPress’s ability which has the sky limit… That is why we love WordPress.

  5. Andrew
    Andrew Published |

    You should definitely throw it out as a plugin. It makes more sense as a plugin than as part of a theme as I think once someone has done this, if they switch themes, they are likely to want to continue that customisation.

  6. Kim
    Kim Published |

    I agree, you should do the plugin.

  7. mercime
    mercime Published |

    Hello Justin. I was just looking over the Custom Post Template Plugin at http://wordpress.org/extend/plugins/custom-post-template/ a few minutes ago. I was thinking that I would prefer a non-plugin solution 😉
    Thank you for your post!

  8. rastaman
    rastaman Published |

    A good site! What is the name of u theme? I want the same)

  9. J Mehmett
    J Mehmett Published |

    @ Justin Tadlock,

    True. CSS can be implemented to change the look and feel of certain posts, using post IDs as classes as usual.

    But, I thought of how someone would use the technique explained in this tutorial, of course someone may use it when they want to change the normal content of the sidebar, navigation, header, footer, etc… Or when they want to add something extra in the posts template…

    Great tip though. I may need it someday.

  10. Dian
    Dian Published |

    Could be this done in the rss feed? if yes could you explain it in the next tutorial 🙂
    btw i was looking for tutorial of this kind of topic a couple of days ago, but none could explain it this better,

    thx a lot for the tip

  11. Dian
    Dian Published |

    I’m sorry, what i meant that by reading this tutorial i understand that u could sort a page into certain category post(using string $cat right?), I thought by sorting it, u could divide the site into a new rss feed setting, the new page have another feed sorting but i forgot that this is a SINGLE post template, so my bad 🙂

  12. Galwin
    Galwin Published |

    Hi Justin! I’m wondering if you like the idea of having an entry about creating sidebar template for Wordpress?

    I liked this post.

    Stay fit!

  13. Galwin
    Galwin Published |

    Thanks for your answer, Justin. I read the ‘kill the sidebar’ entry and I feel you now.

  14. Links XXVII - Peter Kröner - Die Kunst des Machbaren
  15. Andrea
    Andrea Published |


    Most interesting article.
    Your solution may be exactly what I was looking for.
    I’m going to open a blog in collaboration with a couple of friends and we were looking for a way to selectively display adsense ads. Every blog post single page should display only the code relative to the adsense account of the author of the post.

    I think your tutorial can solve our problems.

  16. Toks
    Toks Published |

    Thanks for the post. I tried and it works but when I make any changes or save/publish it takes me to a blank screen. Do you know why that is?

  17. Website Builders Resource Carnival #1 | Website Builders Resource
  18. Michael
    Michael Published |

    This it what I was looking for:D


  19. desi
    desi Published |

    I tried it and it goes to blank screen do you have any suggestions how do I make it work please? or should I post the error here?

    Thank you

    1. Huroman
      Huroman Published |

      @Desi and others who ask about the white screen/blank page.

      It’s easy to fix it. When “function.php” code has been modified occurs some problems whith extra-spaces between the code lines. Just delete that spaces and the Admin will save it as normal.

  20. marvs
    marvs Published |

    Thanks for the post, but I have a question, how to use your function “my_single_template($single)”? what is $single parameter return. please help.. thanks

  21. marvs
    marvs Published |

    Please help.. Fatal error: Call to undefined function add_filter() in C:\xampp\htdocs\wp_cfip\wp-includes\functions-custom.php on line 12

  22. marvs
    marvs Published |

    hi., thanks 4 the reply. I’m a newbie in wordpress, can give me a value for $single parameter (e.g. my_single_template(“?”)). One more question, whay there’s an error occured when i’m trying to call add_filter() function. Thanks again.

  23. marvs
    marvs Published |

    sorry.. I’m newbie in forum, nway i’ve got the solution. thanks 4 the reply again.
    – marvs (philippines)

  24. Jon Kristian
    Jon Kristian Published |

    I was just looking for something like this, very useful. What I’m looking to do is a catch-all single post template for subcategories aswell.

    You’ve got some awesome tutorials!

  25. Gabriel Lerner
    Gabriel Lerner Published |

    Hi Justin
    You wrote “They should not be used to change the look (style, design) of a particular post though. You can do this with CSS.”

    J Mehmett also said:
    “CSS can be implemented to change the look and feel of certain posts, using post IDs as classes as usual.”
    How exactly do I assign different styles in CSS to different template?
    For example, I would like to have different styles for different authors, after creating different post templates for each of them.

  26. Jessica Manson
    Jessica Manson Published |

    nice to use it for subcategories!!

  27. Cory Glauner
    Cory Glauner Published |

    This works, but when I try to edit something now I get the following error:

    Do you know how to fix this?

    Some of this comment was deleted by the admin. Please properly format code before posting it in a WordPress comment section.

  28. Dagi
    Dagi Published |

    This tutorial is for someone who is familiar with wordpress, i want to learn more about
    wordpress, but don´t know if i should use it or not, cause sometimes i hear there are
    many security risks to use wordpress? Don´t know exactly, so i have to check it before
    finding out the clue

  29. dan
    dan Published |

    amazing and thank you. when i find some extra time i’m really going to dig into the usefulness of functions.php. wordpress continues to impress me with its flexibility and ease of implementation.

  30. eric
    eric Published |

    Hey, EXCELLENT little tutorial. very nice and exactly what i was looking for.

    quick question: would it be possible to add a Login template to this? I’m not sure how to check for login and integrate it into your existing code. Any thoughts on this? I’d love to be able to rebuild wp-login.php and it would be an excellent addition to the custom template concept.

  31. Joe
    Joe Published |


    Justin, can you give us an idea of how to use this technique so that it works with sub-categories? Is it necessary to write a new function?

    Many thanks!

  32. Erin
    Erin Published |

    Awesome tutorial. I am in the process of designing a site that has 2 top-level categories with different templates, and was trying to figure out a way to give single posts matching templates depending on the top-level category. Since the site has many levels of sub-categories, this was becoming rather difficult. It seems using tag-specific single post templates is much more straightforward than finding a way to get the top-level category and assign a single post template that way. Thanks for the info!

  33. Rob
    Rob Published |

    Well, I finally got round to doing this on the few posts that warranted it and its yet another powerful tool you’ve helped us with Justin – a pretty simple, yet elegant solution which I didn’t have to come up with myself! Result. While I say the solutionis simple, by that I mean logical, as opposed to easily made.

  34. JeremiahTolbert.com » Blog Archive » links for 2009-03-24
  35. Jonas
    Jonas Published |

    Great! Works beautifully and so simple to deploy/organise, many thanks!

  36. Javier
    Javier Published |

    Hi all,

    I just wanted to notice that there is a very very simple plugin by Ryan Boren that allows you to get that done. Just in case somebody wants to have a look on it I leave here the link: http://allancole.com/wordpress/5/custom-category-post-template-plugin/

    Thanks for your good tutorial!!


  37. Scott
    Scott Published |

    Thanks! Your tutorial was very clear. But, I was also curious how this would be done with subcategories. Do I have to add or modify the filter?

  38. Scott
    Scott Published |

    I think I just answered my own question if anyone is interested about using your single parent category template to include your subcategories, as well.
    Just add another elseif statement:

    elseif(file_exists(SINGLE_PATH . '/single-cat-' . $cat->category_parent . '.php'))
    return SINGLE_PATH . '/single-cat-' . $cat->category_parent . '.php';
    1. Mansour
      Mansour Published |

      Not working my friend!

  39. Berry Blanton
    Berry Blanton Published |

    This was extremely helpful and super easy to implement! Thank you!

  40. Marc
    Marc Published |

    Hey, great tutorial. Got this up and running in a little experiment I’m undertaking.

    Because many of my posts will have images or extra CSS, I need a tidy way to store these post specific files so my site structure doesn’t become an absolute mess.

    What I need to know is how, instead of searching for single-“post ID” inside the folder /single, search for that template file inside a folder with the same post id.

    I’m simply not sure how to get the POST ID tag to work inside define(SINGLE_PATH.


  41. Marc
    Marc Published |

    A second thought just popped into my head,

    How am I able to tell my homepage (displaying just the single most recent post) to call this individual template file aswell?

  42. Ned
    Ned Published |

    You’re a life saver mate! Thanks heaps. This tutorial helped me a lot.

  43. deMelo
    deMelo Published |

    “necessity is the mother of invention”

    Ok I want to blog about a video today… I better use my post temp for video!
    Now I want to blog about photography… better use my post temp for photo’s!
    Now I just want to write a straight forward post… better use the default!

    Currently I would have to leave the blog timeline and use pages to accomplish this…
    Although all the filter ideas are great… I would simply be happy to see the page templates functionality extended to posts… so I could select a template from a drop down menu as I can with pages…

    I would love to use a plugin that can do this… 😉

  44. deMelo
    deMelo Published |

    p.s… I forgot to mention that each post template would have different functionality… i.e… a video post would have an area at the top of the page for the video (using the “simple-video-embedder”) and the photo template would have a similar area for the photo etc…

    Hope this makes sense… 🙂

  45. deMelo
    deMelo Published |

    “always the last to know…”

    Found the perfect plugin…!!!… “Custom Post Template”

    The WP community is the best on the web…!!! 😀

  46. David
    David Published |


    The above plugin gave me an error so I decided to use this method and I think it’s the perfect solution for me … plugin free!

    I have 3 different single post templates that can be changed at any time with a simple edit of the post tag.

    Thanks Justin!

  47. Justin Goring
    Justin Goring Published |

    Thanks – been searching around for months for a method. You have suplied it, and done it in a way that I could easily follow and impliment. Thank you.

  48. Adi
    Adi Published |

    Hm, I have zero knowledge in PHP.
    But I love to design a template..
    There one thing that confuse me, your function check for name and ID?
    Do you think we need to call both?
    Thanks for the answer..

  49. Membuat Single Post Templates | Adi Mahacita
  50. Emanoel Melo
    Emanoel Melo Published |

    Nice, but how do we make it work with all the posts of a child category. Ex.: I name a single-category-3 and work for my posts on category 3 (Cars). But the child categories posts (Ford, GM, Ferrari) must be the same layout. To make it work I have to copy the file and rename ir several times, everytime I made a change….

  51. Tyler Hauser
    Tyler Hauser Published |

    This is a terrific tutorial but it allows me to ALMOST do what I want. I’m just wondering, is there a way to make a template for a combination of two tags? I want to include specific custom fields on pages tagged with ‘film’ and ‘review’ and I think your above methods are the closest to the easiest way to achieve this. I may be wrong, but it’s my current course of action. 😛

    If I could create a template like single-tag-film-review.php and have it only be used when those specific tags are used, I’d be a ridiculously happy camper. Any help with this would be GREATLY appreciated.


  52. anime izle
    anime izle Published |

    Thank you for this article

  53. ConradH
    ConradH Published |

    Thanks Justin! This tutorial is just what I was looking for.

    For the people wondering how this might be used, I’m making a portfolio site where each project will have multiple blog posts about it but one post will contain the project summary. I want to have a special template for those summary posts that shows the summary, all the images related to the project, lists all the projects blog posts, and has a unique layout to look like a portfolio rather than a blog. I’ll have a landing page for projects that will query just these summary posts and display them portfolio style.

    I’m planning on using a custom field to differentiate the summary posts from the rest so hoping to modify this code for that purpose. Any ideas would be greatly appreciated.

    It looks like the Custom Post Template plugin can probably accomplish this but I’d rather code something I can customize and maintain as needed.

    Thanks again Justin… Best wordpress info blog I’ve found on the web…

  54. Darmoid
    Darmoid Published |

    I need template post adding films with using custom fields its realy?

  55. Ben
    Ben Published |

    Hi Justin,
    I’ve a couple of questions on this approach:

    1. Has get_the_tags changed with the 2.8.2 release? I’d been writing my own single post function and got stuck with get_the_tags returning false all the time, when I’ve tried your code it also seems to return false on the get_the_tags call… any thoughts?

    2. I notice you set the in_the_loop query property false before calling get_the_tags and then reset it after the foreach loop. I thought that might have been my problem for a while, but it wasn’t 🙁

    However I’m confused, if you find a tag template you’ll return without ever resetting the in_the_loop flag to false, and if I read this right your using the global wp_query object – can this ever be a problem?

  56. Joel Smith
    Joel Smith Published |

    Hey Justin,

    You helped me in such a big with this post. So much so I felt like I needed to voice it to you!

    Thanks! 🙂

  57. Maor Barazany
    Maor Barazany Published |

    Great, clear and useful tutorial, 5 minutes to imply!
    Great job.

    I have another question about templates, Is there an option to have a category-id.php for a parent category and having all its childs to use it as well?

  58. Zohra S
    Zohra S Published |

    Thanks so much for this! Was gonna start tearing my hair out to find a way to make this work.
    For the categories filtering, how can I make it work for a category and its children? I’m curious to know if that’s possible.
    Thanks again!

  59. Coafuri
    Coafuri Published |

    UNtil now i create every single post from scrap. I thing half of my time spent is in designin.I will try your advice. Thank you

  60. Darren
    Darren Published |

    Thank you very much for this. Genuinely. It’s so very useful.

  61. Culch.ie » Blog Archive » The Irish Apprentice 2009
  62. Fransgaard
    Fransgaard Published |


    Solved my problem… and I don’t even know how to code PHP 😀

    Thanks for that.

  63. Lee Leslie
    Lee Leslie Published |

    Thanks. Exactly what I wanted. So well explained. Test worked immediately. Hat’s off to you. We are an volunteer journalism site with about 100 writers. We’re going to use it to allow authors to own their page – with their own google ads, donate button, blogroll, rss, etc. Perfect.

  64. Create Custom Single Post Templates for Specific Posts or Sections in WordPress
  65. Jamie Dunne
    Jamie Dunne Published |

    Good tute J,
    ..this is my third time to google this sort of stuff in as many months ..and end up on you blog easch time…
    ..Ive found the plugin that does most of the first part for me … if only i cud get one to design my new /single-002.php template ! LOL

    jamie d

  66. Visual Rhythm
    Visual Rhythm Published |

    It worked! Fan-frickin-tastic!! Thank’s for the useful tip Justin!

  67. Stela
    Stela Published |

    🙂 works great! It as Exactly what i was looking for. I dislike the usage of plugins and this do a great job!

  68. Marg
    Marg Published |

    Thanks so much! I’m a Wordpress newb, creating a site for a client with Wordpress as a CMS, not for blogging. Found the plug-in for choosing a single post template, but did not want to rely on the client remembering to choose it when they enter a new post. Your method means they don’t have to think about it.

    And I’m not a coder, but I was able to follow your step-by-step directions and get it working a just a few minutes. I found some other instructions elsewhere that seemed like they might be what I need, (http://codex.wordpress.org/Theme_Development#Query-based_Templates), but they did not spell out exactly where to paste the code. I’m sure a lot of people here didn’t need every bit of detail, but I did, so thanks for writing it all out.

    Thanks so much for helping me out!

  69. Marg
    Marg Published |

    Follow up: I thought I had this working, but when I came back to the site a day later I was unable to login. Just got a blank screen of death after entering login data no matter what I did. After a good hour or so of panic I thought to delete the edited functions file and replace it with an original, and voila I could login again.

    I’m wondering what I did wrong. Have you had anyone else report this problem? Looks like I will be doing some reading on the functions file.

  70. Benjamin Thomas
    Benjamin Thomas Published |

    Great hack, many thanks for sharing this!

  71. Robbie
    Robbie Published |

    I’m currently using this method on my company’s site. Thanks!

    I want to take things up one level, though, and use different templates for a single post depending on the URL (based on category).

    Let’s say the default for each post is: /post/foo.html
    And I want to use a different template for the private version, which is: /post/private/foo.html

    (It serves up different content using different custom fields.)

    I can use redirect rules to control access to the private version. The trouble is, each post exists in _both_ categories, so it will always use one or the other as the template. I want to use /private/ only when someone goes to that specific URL. Under this current setup, Wordpress is serving up every /post/foo.html using the private template because it also happens to be in the private category.

    Can I fix this with changes to the functions.php file?

    In any case, thanks for all your help.

  72. Duncan
    Duncan Published |

    YAHOOO!!!! It WORK!
    I search for ways to show different single templates by author, then find your tutorial, follow it, and it work! Save me a lot of time.
    Good job Tadlock.
    Thanks you.

  73. a|x
    a|x Published |

    This looks really useful, thanks for putting it out there.

    The use that springs to mind is the scenario I’m trying to implement at the moment- I have a particular category of posts that use custom fields, so I need to have custom templates for displaying the additional meta information for just these particular posts and the archive pages for these posts. Wordpress already seems to have the functionality for custom category templates, but I need to do the same thing at the single-post level, too.

  74. a|x
    a|x Published |

    Oh, and I’d love to see a plugin version of this, if you’ve not already made one. I’ve seen a couple of plugins allowing selection of a particular template at the post-authoring stage, but not one that allows custom templates to be used on a per-category basis.

  75. sam cane
    sam cane Published |

    Excellent walkthrough, easy to follow and implement. thanks very much.

  76. efrem
    efrem Published |

    i am trying to create a post template so that i can use a set template everytime i add a post for my universities i am listing.

    I need to create a template so that when I want to load the individual universities as individual posts I get a custom template to fill in.. the individual unis will be loaded as posts under category ‘university listing’

    eg it will load the headings for the post onto the page and i then jsut paste the text into it and save it.

    http://www.postgraduatesinsport.com/wp-content/uploads/2010/04/BIRKBECK-UNIVERSITY-OF-LONDON.pdf is an example of the layout of what I want to create. It will appear as a post each time… so basically I need a template designed that me or my staff can use to load unis etc…

    Top section will have :

    Uni logo, uni contact details
    Web link

    Mid section:
    The text will be pasted in so just the header need here…
    Would like to have a video inbedded on the right with an interview with the student there… thumbnail video inbedded

    Bottom section:
    Men/Women Ratio
    Careers Students have gone into

    Straightforward really. Just need the headings set into a post template called pageofuniversityposts.php or something like that….

    When I then load a post and select this template, it will load the template headings and we just need to paste the text and insert the logo and video and away we go!!

  77. a|x
    a|x Published |


    I’m revisiting this excellent tutorial.

    Just wondering if you could give me some tips on extending it slightly.
    I’d like to also be able to check if the post has a particular custom field value, and use that as the path to a custom single post template.

    I’ve tried this:

    But it doesn’t seem to work for the custom field value (the category-checking works fine though). Any ideas?

    Thanks again for sharing,


  78. Jodi
    Jodi Published |

    I was about to give up and use a plugin when I found your wonderful tutorial. Thank you for sharing your expertise.

  79. Jason
    Jason Published |

    First, I love it. Second, I am WILLING TO PAY for support…
    The problem, I have my site set up with custom categories… This page, http://hellodestroyers.com/new/friends/, list all the post from the friends category, and the permalink should connect the user to the appropriate single post, however, it loads the most recent post of that category?

    Again I will pay for support?

  80. Jason
    Jason Published |

    Never mind.. I removed from a seperate loop with in the same page and BAAM… good to go.

  81. Benjamin Langlois
    Benjamin Langlois Published |

    Thank you very much for posting this – just what I’ve been looking for; to be honest I didn’t realise this was even possible in WordPress. Have now implemented it and it is running perfectly.

  82. Monica
    Monica Published |

    Thank you this worked like a charm. See it in action at http://www.rocheport.com/new (while I’m developing) and later simply at Rocheport.com

    Thanks again, this was EXACTLY what I was looking for.

  83. Travis
    Travis Published |

    Thanks Justin for the amazingly simple tutorial. You never fail to deliver.

    In theory, your “single template by ID” statement inside your my_single_template function could be used for pages as well?

    If page ID = 2, single-2.php?

  84. Using WordPress Categories To Style Posts « Lorelle on WordPress
  85. Stop Letting Sponsors Determine Your Blog Layout! | Philaahzophy
  86. Larry Levenson
    Larry Levenson Published |

    This 2+ year-old post was a life saver! Thanks! Spent several hours trying to make a custom post template work — to no avail. I walked through this post step by step, and now my Hybrid child theme has working post templates. Thank you!

    By the way, I’m running this on WP Multisite 3.0.3 and it works great!

  87. Kostas Kontos
    Kostas Kontos Published |

    Hi Justin, I recently find this useful method for post templates and I’m wondering if there is a way to work this trick for me.

    So if we have one post in two or more categories and we want to display the post with comments ability in one category and without comments in the other categories. Is this possible?


  88. jeff campagna
    jeff campagna Published |

    Hey! Great Article, Justin!

    What if the element you want to change is not in the single.php file but in the loop.php file? The loop calls the post thumbnail to appear before the post title. But I want, for a specific category, to remove that thumbnail from the loop…

    Hopefully I am making myself clear.
    Thanks so much for this!

  89. Christopher Wulff
    Christopher Wulff Published |

    Love this and have used it a few times, particularly for the category-specific templates. With a new site using custom post types though, I am encountering a problem with the default use of single-{post-type} not returning the single template for the custom post type.

    How would I modify the code above to maintain the category specific single post templates while also enabling the single post type ones?

    1. Christopher Wulff
      Christopher Wulff Published |

      Got my question answered on WordPress StackExchange here: http://wordpress.stackexchange.com/questions/13837/conflict-in-function-to-allow-single-post-template-based-on-category

      Here’s the code:

      /* Define a constant path to our single template folder */
      define(SINGLE_PATH, TEMPLATEPATH . '/single');
      /* Filter the single_template with our custom function*/
      add_filter('single_template', 'my_single_template');
      /* Single template function which will choose our template*/
      function my_single_template($single) {
          global $wp_query, $post;
      /* Checks for single template by post type */
      if ($post->post_type == "POST TYPE NAME"){
          if(file_exists(SINGLE_PATH . '/single-' . $post->post_type . '.php'))
              return SINGLE_PATH . '/single-' . $post->post_type . '.php';
      /* Checks for single template by category. Check by category slug and ID */
      foreach((array)get_the_category() as $cat) :
          if(file_exists(SINGLE_PATH . '/single-cat-' . $cat->slug . '.php'))
              return SINGLE_PATH . '/single-cat-' . $cat->slug . '.php';
          elseif(file_exists(SINGLE_PATH . '/single-cat-' . $cat->term_id . '.php'))
              return SINGLE_PATH . '/single-cat-' . $cat->term_id . '.php';
      /*Checks for default single post files within the single folder */
      if(file_exists(SINGLE_PATH . '/single.php'))
          return SINGLE_PATH . '/single.php';
      elseif(file_exists(SINGLE_PATH . '/default.php'))
          return SINGLE_PATH . '/default.php';
      return $single; 

      and just replace “POST TYPE NAME” with your custom post type name.

  90. skybondsor
    skybondsor Published |

    This is still great over two years later! Thanks for posting it.

  91. Random Joe
    Random Joe Published |

    does checking for author by user nicename and user ID work on wordpress 3.1.2?

    I’m trying to make this work on twentyten theme. I cant seem to make it work.

  92. Random Joe
    Random Joe Published |

    Can you upload a theme folder that works on wordpress 3.1.2. I really need the method for checking a template by author.


  93. Euge
    Euge Published |

    Thanks Scott for your code! But I found a problem, what about sub-sub categories? Or sub-sub-sub-sub categories? 😀
    This is the complete code that I added to my functions.php

    function pa_category_top_parent_id ($catid) {
     while ($catid) {
      $cat = get_category($catid); // get the object for the catid
      $catid = $cat->category_parent; // assign parent ID (if exists) to $catid
      // the while loop will continue whilst there is a $catid
      // when there is no longer a parent $catid will be NULL so we can assign our $catParent
      $catParent = $cat->cat_ID;
    return $catParent;
    //funcion para mostrar un single template para cada categoria
    * Define a constant path to our single template folder
    define(SINGLE_PATH, TEMPLATEPATH . '/single');
    * Filter the single_template with our custom function
    add_filter('single_template', 'my_single_template');
    * Single template function which will choose our template
    function my_single_template($single) {
    	global $wp_query, $post;
    	* Checks for single template by category
    	* Check by category slug and ID
    	foreach((array)get_the_category() as $cat) :
    		$categoriapadre= pa_category_top_parent_id ($cat);
    		if(file_exists(SINGLE_PATH . '/single-cat-' . $cat->term_id . '.php'))
    			return SINGLE_PATH . '/single-cat-' . $cat->term_id . '.php';
    		elseif(file_exists(SINGLE_PATH . '/single-cat-' . $categoriapadre . '.php'))
    			return SINGLE_PATH . '/single-cat-' . $categoriapadre . '.php';	
    return $single;
  94. Matej Latin
    Matej Latin Published |

    thanks for this.. I didn’t know it’s so simple.. and I like it simple 🙂

  95. Jez
    Jez Published |

    Can this be set up for single files assigned to a Custom Taxonomy?

    As currently its just for default categories…

    Thanks 🙂

    1. Brad
      Brad Published |

      I think I got it, just change ‘taxonomy_name’ to whatever your custom taxonomy is called.

      	* Checks for single template by category
      	* Check by category slug and ID
      	foreach((array)get_the_terms($post->ID,'taxonomy_name') as $cat) :
      		if(file_exists(SINGLE_PATH . '/single-cat-' . $cat->slug . '.php'))
      			return SINGLE_PATH . '/single-cat-' . $cat->slug . '.php';
      		elseif(file_exists(SINGLE_PATH . '/single-cat-' . $cat->term_id . '.php'))
      			return SINGLE_PATH . '/single-cat-' . $cat->term_id . '.php';
  96. Brad
    Brad Published |

    I’m with Jez, is it possible to modify it to pull a template based on a Custom Taxonomy?

  97. Custom Field, Taxonomy Post Page Template | Q&A System
  98. Sandeep Yadav
    Sandeep Yadav Published |

    I want to make one post template for my job based website and i will try with your codes and hope it will work for me on my website. Thanks for this tutorial.

  99. Mahesh
    Mahesh Published |

    I was just looking to make a simple template file for a particular post. I just wondered why it was not so easy as making it for a particular page and trying out numerous things

    I am glad to know how to create it by your article. Thank you

  100. Extending WordPress' Template Hierarchy with Single Post Templates | Redact Web
  101. Blog Markus Steiger » Wordpress Links
    Blog Markus Steiger » Wordpress Links at |
  102. kriskl
    kriskl Published |

    Hi Justin

    I am late for the party, only now I got around trying to create single post templates..

    your solution is great, but my problem is, it does not include support subcategories,

    so, it means, I would have to create 20-30 single post templates which would be impossible to maintain 🙁

    any idea how to add sub categories support?


  103. Duncan Michael-MacGregor
    Duncan Michael-MacGregor Published |

    Could this method be modified to handle custom Taxonomies?

    If so how could it be done?

    Cheers 🙂

  104. Sergio fernandez
    Sergio fernandez Published |

    Great!! thanks dude!this is exactly what I was looking for.

  105. - Enemy of the State at Enemy of the State
    - Enemy of the State at Enemy of the State at |
  106. andy
    andy Published |

    Thanks Justin – this is really useful, particularly for large sites that may need specific tailoring of individual posts; an example – we have a large news site that wanted to tailor a specific post’s sidebar and layout for seo purposes.

  107. Gino Côté
    Gino Côté Published |

    I want to use it to show or not the sidebar in the single page. Do you thing it’s the best way to do it. I also saw new theme who use a options for page (not for post) under the text editor to choose between two thumbnail with radio button representing full page or page with sidebar

  108. Mathew
    Mathew Published |

    by function.php do you mean the one in the root of theme or inc/function?

    because when I add the code to former nothing happens and when I add it to latter the code appears on top and every page and functions doesnt work.


Leave a Reply

By submitting a comment here you grant this site a perpetual license to reproduce your words and name/Web site in attribution.

Please use your real name or a pseudonym (i.e., pen name, alias, nom de plume) when commenting. If you add your site name, company name, or something completely random, I'll likely change it to whatever I want.