DiggingIntoWordPress

by Chris Coyier & Jeff Starr

WordPress 3 Template Hierarchy

Posted by on

This chart is one entire page in our book, but I thought it would be good to focus on specifically. Template hierarchy has gotten a bit more advanced since the last time we covered it.

The idea is that WordPress will look for files in a theme to use to render the current page in a specific order. For example, let say you have a page for showing posts from a certain category like this. On this site, all our category pages are currently the same and use “archive.php” template. This file is pretty far down the hierarchy, and is shared with other types of views, for example, tag pages.

If we wanted to differentiate our category pages from our tag pages, we could create a file in our theme called “category.php” and our site would instantly start using that template for these pages, without us having to do anything else. We could get even more specific, have theme files that are unique to the categories themselves! To make a page specific to our security category, we could make a file called “category-security.php” and our site would use that file when viewing that category page.

You could really get fancy with customized pages by having a good understanding of the template hierarchy! Do take care not to get too overly repetitive with your templates though. If you have 10 categories and want to have a different header on each, you may be better off using a single category.php file with conditional tags rather than 10 separate files.

SEE THE FULL SIZE IMAGE

This was based on this hierarchy list that was sent to me by Ptah Dunbar. Thanks Ptah!

  • 404
    1. 404.php
    2. index.php
  • Search
    1. search.php
    2. index.php
  • Tax
    1. taxonomy-{tax}-{term}.php
    2. taxonomy-{tax}.php
    3. taxonomy.php
    4. archive.php
    5. index.php
  • Home
    1. home.php
    2. index.php
  • Attachment
    1. mime.php
    2. type.php
    3. mime_type.php
    4. attachment.php
    5. single.php
    6. index.php
  • Single
    1. single-{post-type}.php
    2. single.php
    3. index.php
  • Page
    1. custom_template.php
    2. page-{slug}.php
    3. page-{id}.php
    4. page.php
    5. index.php
  • Category
    1. category-{slug}.php
    2. category-{id}.php
    3. category.php
    4. archive.php
    5. index.php
  • Tag
    1. tag-{slug}.php
    2. tag-{id}.php
    3. tag.php
    4. archive.php
    5. index.php
  • Author
    1. author-{author-nicename}.php
    2. author-{author-id}.php
    3. author.php
    4. archive.php
    5. index.php
  • Date
    1. date.php
    2. archive.php
    3. index.php
  • Archive
    1. archive.php
    2. index.php

20 Responses

  1. Thank you for this post. It’s very useful!

  2. Fikri Rasyid September 12, 2010

    How about “home” of custom post type? We can use home.php or index.php for displaying our latest post. But which template will be used if we have custom post titled -for example- book? Furthermore, how’s the permalink’s structure?’

    • Archive/index pages for custom post types will be coming in 3.1.

      • Paul Kazmercyk September 13, 2010

        Andrew: So WP 3.1 will be adding to the existing template hierarchy to accommodate custom post types? Will it be something like mycustomposttype.php > archive.php > index.php?

  3. You forgot front-page.php which WordPress looks first for the homepage.

  4. This is a really useful chart, thanks!

    To compliment the template conversation, it’d be awesome to have some tutorials that go through the new Twenty Ten theme, especially the monster (but nicely commented) functions.php file. These new PHP functions such as _e, __ and esc_attr_e are confusing! It’d be great to know what’s essential to a new theme and what’s not.

    • As you might have guessed, those are for translations. Kubrick (the default theme from WP 1.5 to 2.9) was distributed in two formats — hardcoded for the U.S. English crowd, and internationalized for everyone else. Aside from forcing us to maintain two different copies and it isn’t teaching best practices.

      You can learn more about translations here (though that page could probably use an refresh). For the most part, it’s one main function, __(), with a bunch of shorthand aliases to make things easier. The function _e() is actually just echo __(). And esc_attr_e() is just echo esc_attr( __() ).

      Glad you like the comments :-)

      • Thanks for the reply, Andrew! Yeah, this new functions.php file is overly complicated for me but I’m determined to at least learn a little more about how I can slim it down b/c I’m positive I’m not using everything and I like to optimize themes for speed.

        I’d really love to see a play-by-play for each of the code functions in the functions.php file and how/where they relate to the code in the rest of the theme template files and the origin of different functions. For instance, I see something like this __( '<em>alias</em>', 'twentyten' ) or <?php _e( '<em>description</em>', 'twentyten' ); ?> and I’m unsure of where twentyten is established and what the alias or description means or does, even with the excellent comments you left.

        It’d also be great to know which code is essential/highly recommended/optional.

  5. Using single files really helps keep the code seperated IMO. I prefer this. Another thing that wasn’t said about this is vs seperate conditional tags is that if you screw up the code you only have to debug one file instead of having to go through a lot more code to debug.

    Your book rocks and I am glad you keep adding to the information via this website! I wish all ebooks publishers did this!

  6. Nice, info graphic. I like the way you cascaded them because a lot of people don’t understand that there is more than one path for templates files to get the same result.

  7. Nice one, but typo in the heading!
    “Heirarchy”

    • Ah yes I see where you mean. We’re just fixing the final proof of the print version now, so good timing, I’ll make sure that’s fixed.

  8. good

    i hope WordPress will modif dynamic inclusion such as including footbar.php from theme files by calling function get_footbar()

    or leftsidebar by calling function get_leftsidebar()

  9. Great post! The “Which Template File Will WordPress Use” chart makes it much easier to understand. Big thank you from a visual person :)

  10. This one is called knowledge sharing.
    Thanks a lot for your work.
    It will reduce lots of time for template implementation.
    Big Thank you for this.
    Avi

  11. So this is definitely the best explanation/infographic I’ve seen of this concept. Very useful, thanks. However, can anyone clearly define the different use cases for when to use a page/post-specific template (e.g. page-{id}.php) and when to select a template from the Page editing interface?

  12. You do no, that there is a official template hierarchy graph from the wordpress codex? http://codex.wordpress.org/images/1/18/Template_Hierarchy.png

    • As I’m sure you saw while looking at that link, it’s not updated for 3.0.

      • What are the differences? I don’t think I see any.

        Or did they update it since this was last written?

        P.S. loving digwp!

  13. What happened to single-{ID}? Is that no longer checked before single-{post-type}?

Comments are closed. Contact us with any critical information. Thank you!

Code is poetry