Gravity Forms Plugin for WordPress

77 responses to “Creating single post templates in WordPress”

  1. Landing Page Templates

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

  2. Jean-Baptiste Jung

    Excellent tutorial. Thanks for sharing your knowledges, Justin!

  3. Lyndi

    A tricky topic well explained. Very nice tutorial.

  4. J Mehmett

    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

    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

    I agree, you should do the plugin.

  7. mercime

    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

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

  9. J Mehmett

    @ 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

    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

    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

    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

    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

    [...] Creating single post templates in WordPress – Praktisch! [...]

  15. Andrea

    Justin.

    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

    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

    [...] presents Creating single post templates in WordPress This first part will check for templates within the /single folder labeled like single-100.php . [...]

  18. Michael

    This it what I was looking for:D

    usefull!

  19. desi

    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

  20. marvs

    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

    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

    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

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

  24. Jon Kristian

    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

    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

    nice to use it for subcategories!!

  27. Cory Glauner

    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

    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

    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

    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

    <>

    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

    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

    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

    [...] Single post templates for WordPress (tags: tutorials wordpress php template) [...]

  35. Jonas

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

  36. Javier

    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!!

    Javier.

  37. Scott

    Justin,
    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

    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';
  39. Berry Blanton

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

  40. Marc

    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.

    Thanks!

  41. Marc

    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

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

  43. deMelo

    “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

    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

    “always the last to know…”

    Found the perfect plugin…!!!… “Custom Post Template”
    http://wordpress.org/extend/plugins/custom-post-template/

    The WP community is the best on the web…!!! :D

  46. David

    http://wordpress.org/extend/plugins/custom-post-template/

    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

    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

    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

    [...] Sumber dari situs Justin Tadlock dengan nama Creating single post templates in WordPress ) Leave a Reply 2 views, 2 so far today [...]

  50. Emanoel Melo

    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

    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. :P

    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.

    Thanks,
    Tyler

  52. anime izle

    Thank you for this article

  53. ConradH

    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

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

  55. Ben

    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

    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

    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

    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

    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

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

  61. Culch.ie » Blog Archive » The Irish Apprentice 2009

    [...] note: This post has been hugely helpful in creating workable Wordpress single.php files. tagged under: [...]

  62. Fransgaard

    Brilliant!

    Solved my problem… and I don’t even know how to code PHP :-D

    Thanks for that.

  63. Lee Leslie

    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

    [...] if you have a lot of posts already published in that category. The automated method was shared by Justin Tadlock, one of the very famous WordPress theme [...]

  65. Jamie Dunne

    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

    -Regards,
    jamie d

  66. Visual Rhythm

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

  67. Stela

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

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 (e.g., 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.

Recommended Purchases

Gravity Forms Plugin for WordPress 'Digging Into WordPress' book for WordPress users

WordPress-o-Sphere

  • WordPress 2.8 – "Baker"
    WordPress 2.8 adds hundreds of bug fixes, adds cool functions for themes, boasts a new widget API, and makes custom taxonomies easy for end users.
  • WeRockWP
    WeRockWP recognizes and showcases the individuals who contribute much to the WordPress community. These individuals are called WordPress Rockstars.
  • wpazo
    The all-signal, no-noise, source for the best WordPress stuff.
  • BuddyPress 1.0 has arrived
    The long-awaited BuddyPress (a set of plugins that turns a WordPress MU install into a social network) has been officially released.
  • BuddyPress for WordPress (not MU) coming
    It looks like BuddyPress will also be released for normal WordPress installs (not just WPMU).