Get to Know the WordPress Hierarchy

WordPress’s popularity and usefulness as a multi-purpose content management system, continues to grow, and the more people flock to using this CMS the more an understanding of some of the basics of how it operates becomes necessary.

In that vein, I have put together this post that examines the WordPress template hierarchy and the use of conditional statements. Technically you only need two files for a WordPress theme to work, index.php and style.css.

However, if you would like to make your theme a bit more unique and have some variance in the way different content is presented, you will want to use some other template pages. Now this is where the hierarchy comes into play, because WordPress runs off a Template Hierarchy.

What this means is that it will automatically look for a particular file when retrieving a page in the active theme directory. If, for whatever reason, it cannot find that file, WordPress will then look for the next filename in the hierarchy and so on until it finds a match.

Below I will go over different additions that can be made, that will not only add to the complexity of the overall site, but each one will also impact the site’s hierarchy. With each entry below, I will tell the benefits of having that particular template page as part of your site and how each will alter the hierarchical structure of your WordPress theme.

Home

Establishing a home.php file will give you a custom home page for your WordPress site that varies from your index. This could also be accomplished with conditional tags but I only recommend that for minor changes from the original index.php page to keep size and complexity to a minimum. So if you plan to have major changes in the home page for your site, then adding a home.php is the route to pursue.

The Hierarchy

  1. home.php
  2. index.php

Single

Given that a good number of WordPress installs are used for blogs, you will most likely have a single.php template for your individual posts. This will allow the posts on the blog to have a distinctively different set-up than the other pages on the site, as well as allowing a section for comments.

The Hierarchy

  1. single.php
  2. index.php

Archive

This addition to the template pages effects several other hierarchies for the site. Archive.php controls the presentation of your archived data, such as categories, tags, dates & authors. The addition of an archive.php will be called upon in the absence of the next few template pages that are discussed. Adding this to your templates aids the users in finding the content that is generated for the website with ease.

The Hierarchy

  1. archive.php
  2. index.php

Category

This is a great template to add to your site for the benefit of your user’s and for more specific site and content navigation. You can also go even further and set up a specific look for each individual category that you have your site’s content divided into by creating a category-id.php. Also know that any -id.php template will take top hierarchical priority if it is included.

The Hierarchy

  1. category-id.php
  2. category.php
  3. archive.php
  4. index.php

Tag

Some users in the blog design community opt to focus more on tags rather than categories for separating and organizing the content for the site. If this is the case for the WordPress site you are designing, then you will want to include the tag.php template. And once again, you have an option to style each tag individually by adding the tag-id.php template.

The Hierarchy

  1. tag-id.php
  2. tag.php
  3. archive.php
  4. index.php

Author

If you are designing a blog that is going to have multiple authors contributing, then you can add an author.php template to allow site users to search the site by the authors. You also have the option of adding an author-id.php template if you would like to style each author’s page for that specific author for a more personal feel. Say altering the colors specifically for that author, or however you choose for their page to stand out from the others, you can do that with this template.

The Hierarchy

  1. author-id.php
  2. author.php
  3. archive.php
  4. index.php

Date

Another searchable content element that you can add to the site for the sake of your user’s navigation is allowing them to browse your content by the date that it was published on the blog. If this is needed for your WordPress site, simply add a date.php template to allow this functionality.

The Hierarchy

  1. date.php
  2. archive.php
  3. index.php

Search

Whenever a user searches your site, you can set up a search.php template to control the way that the site delivers the search results to the content seeker. Style it differently than the other templates on the site for a unique delivery and presentation to make it stand out.

This isn’t required and a lot of sites leave it out, but it can always be a nice stylish touch for the site. The default search results from an index.php file look nothing like what a user would expect from a search page!

The Hierarchy

  1. search.php
  2. index.php

404

Style-matching, helpful 404 error pages are an extremely popular way to manage user navigation when errors occur. This helps to ensure that the user who has gotten lost in the site does not necessarily leave out of frustration.

Simply add the 404.php template to make this possible for the client. Here are some ideas to get you started with 404 pages.

The Hierarchy

  1. 404.php
  2. index.php

Page

In order for your site to have multiple pages (i.e. about, contact, etc.) which most will, you need to add the page.php template. Again, this is an area where you can actually individually set up your pages with a page-id.php template addition.

Pages can also have custom templates, the most common use for custom templates is an archives.php page (not to be confused with archive.php. You can read more on how to create a great archives.php file here.

To create a custom template simply place this at the top of the pagename.php file, where ‘pagename’ is whatever name you have chosen.


The Hierarchy

  1. custom.php
  2. page-id.php
  3. page.php
  4. index.php

Conditional Tags

Conditional Tags are another tool at the coder’s disposal when assembling the WordPress framework you are putting together for your client. With conditional tags you can tell WP to only present content if a certain page is being displayed.

For example, is-home() is a conditional tag telling WordPress to only display the selected content on the home page. If this is in your index.php file, that content will only be shown on the home page and not any other pages that default to index.php.

The following is an example of a conditional statement.

if (is_home()) {
   echo 'Welcome to my blog, I hope you enjoy what you read here!';
}

Conditional tags are best used when you only want a minor variation for certain content types, e.g. if you just wanted to display the words "Search Results" at the top of your search pages, you could just add a conditional tag to the index.php file. That way if you update something in the index.php file, you don’t need to worry about updating the search.php file as well.

For larger changes though, conditional comments get messy and you’ll find template files make things easier to manage!

There are a variety of different conditional tags and tons of uses for each. For a complete list and description of each see the WP Codex Conditional Tags Entry

The Visual


Download the PDF

Your Turn

That concludes my look at WP hierarchies, but I would love to hear your thoughts and suggestions in the comment section below on any areas that you need further clarification or that you would do a little bit differently.

Share

  1. Thanks a lot Angie, this is really great. While the wordpress documentation has all of this information, your version is much more concise and easy to read / follow.

    I will be bookmarking this as a future reference.

  2. Logobird (1 comments)16 March 10

    Great stuff Angie. The way you have explained it here is easy to understand.
    Much appreciated.

  3. Angie Bowen (64 comments)16 March 10

    Thanks guys. I’ve always found the WP codex both helpful as well as a bit confusing so I often have to break the info down myself to get a full understanding of it. I’m glad to help others with it as well :)

    • HD Guy (12 comments)28 August 10

      Many thanks for the PDF, helped me understand the wp template structure much better

  4. Vic (1 comments)16 March 10

    The WordPress codex is definitely a rich resource but it sure lacks details sometimes. This article is a must read for starters who’d like to engage into WordPress theme development. Thanks for taking the time to write it.

  5. G13Media (1 comments)16 March 10

    Big thanks, wish I would of found this prior to learning how to create templates. Article is not long but explains everything so well, thank you for explaining things in an easy to understand way.

  6. jo (4 comments)16 March 10

    Great article … I like to learn some think with my coffee in the morning! really easy to read and learn!

  7. jay design (12 comments)16 March 10

    Thanks! This is a great help. I’m quite familiar with WordPress now but it’s still nice to have a concise breakdown of all the elements. Thanks again

  8. So that’s how it all works out! This is a great piece on the ever-growing wordpress platform. I’ve been using it for some time and 20+ blogs but never fully understood some of the technicalities behind it. This makes sense and while not groundbreaking, it’s extremely good to know!

  9. dave (17 comments)16 March 10

    Nice, great visualisation. I was messing with author-id.php the other day on a thematic child theme and came up against something strange yesterday.

    I’d copied author.php into the child theme folder, and then created an author-03.php file in the same folder, but then weirdly all author pages on the site began using the author-03.php file.

    Any ideas why that might happen?

  10. Bookmarked. I am beginning wordpress theme development and this guide is very helpful to build some complex themes.

    Thanks

  11. keukens (2 comments)17 March 10

    Finally some useful resources for wp. Thanks

  12. Great work. I like the visual. Copy and print. thx a lot

  13. I am new to developing or Wp after being a Joomla kinda guy for a long while, I have to say this is the single best post I’ve read on the matter as it shows me where to put my designs and code . Thank you!

  14. Pixil (1 comments)18 March 10

    Very nice breakdown, didn’t understand how wordpress was automatically generating indexes when developing a custom theme. So sidebar.php doesn’t get any hierarchy as it’s not a required page and doesn’t have dependencies?

  15. Josh Stauffer (11 comments)18 March 10

    As simple as your visual is, it’s one of the best I’ve seen. Very easy to see and comprehend the flow. Good one!

  16. zico (9 comments)18 March 10

    This visual hierarchy is very useful. Thanks again.

  17. Great explanation of WordPress hierarchy. This is something every WP theme developer should know. Also very simple and easy to understand for everyone.

  18. Nina (4 comments)20 March 10

    extremely helpful article, thanks for posting :D

  19. great article, thanks!!! John

  20. Mitko (26 comments)20 March 10

    Sorry to be the only one going negative on the article :) , but can be helpful/interesting only for a developer.

    As for a blogger, and site owners with WP, especially using high customization themes such as Thesis, they won’t really need to learn even how to spell wordpress :D

  21. Robin Rath (1 comments)21 March 10

    Great article, this came in extremely handy when we built out our site.

  22. Angie, nice list. I’ve searched through the WordPress codex several times to make sure I was clear on the hierarchy. Do you see 3.0 having any major changes to the hierarchy?

  23. endiyan (1 comments)22 March 10

    superb article. a very nice brief article for a wordpress-noob!

  24. So helpful, Thank You so much !!!!!!

    :)

  25. Very helpful article, visual illustration is awesome… thanks so much :)

  26. Randy (2 comments)26 March 10

    You can change your home page from a list of posts to a static front page on the Settings > Reading page. Interestingly enough, is_home() and home.php do not necessarily reference the home page: they actually reference the page that is the list of posts. If you set a static front page, then the home page will use page.php just like all other static pages. The conditional is_front_page() will always reference the home page, whether that is a list of posts or a static front page.

  27. jeffrey (2 comments)26 March 10

    Very well done! Its nice to learn the visual way for once. WordPress does have help files and so on but its not the same.

    Thanks again!

  28. Great article, this came in extremely handy when we built out our site.
    This is something every WP theme developer should know. Also very simple and easy to understand for everyone.

  29. I’d copied author.php into the child theme folder, and then created an author-03.php file in the same folder, but then weirdly all author pages on the site began using the author-03.php file. thanks

  30. Um agree with you, If you set a static front page, then the home page will use page.php just like all other static pages. The conditional is_front_page() will always reference the home page, whether that is a list of posts or a static front page. thanks

  31. Ttemplate (1 comments)30 March 10

    Great article, can you explain how to create different archive templates for different categories?

    Thanx

  32. Doman (1 comments)2 April 10

    How do you imagine your death?

  33. Luana (1 comments)2 April 10

    This is great stuff , so clear, well explained, thanks for sharing!

  34. Great stuff!!! Already bookmarked for future consultant. Thanks for share this knowledgment.

    Very usefull

  35. Thanks a lot for the reference.

  36. Great llittle article, lots of useful information, but short and to the point. Also found out about a few new template files I can use from now on (Just getting into theme development)

    One thing you might want to add to your post, is that there is a new file which can be used for categories introduced in WP2.9, which is category-name.php and that takes the top spot

    1. category-slug.php (Note: available with Version 2.9)
    2. category-ID.php
    3. category.php
    4. archive.php
    5. index.php

    Taken from: http://codex.wordpress.org/Category_Templates

  37. zoel (2 comments)13 April 10

    thanx for your reference. it’s help me to dev some wp theme ;-)

  38. WP Tricks (7 comments)15 April 10

    For WordPress Themes developer.. this information is really useful

  39. Baju Muslim (1 comments)19 May 10

    Hi Angie, I learn that some WP themes only have an index.php with its multi-function to represent outputs of a regular page.php, single.php, category.php, archive.php, etc. Of course it comes with a bunch of if-elseif-else statements to work properly.
    I just wonder, in terms of speed and performance, which theme system is more efficient?

  40. Finally some useful resources for wp. Thanks

  41. WordPress’s popularity and usefulness as a multi-purpose content management system, continues to grow, and the more people flock to using this CMS the more an understanding of some of the basics of how it operates becomes necessary.

  42. I am very interested in your article, thank you!

  43. actually it is the point of understanding wordpress and wordpress theme

  44. megainfo_dz (1 comments)7 July 10

    Hi !

    How about register.php , signup , and fwpassword LoL

    thanks

  45. Excellent explanation, helped me a lot because i am a noob in WordPress but i believe over all it is like most websites on the internet. Thanks for the information.

  46. TechGopal (1 comments)9 July 10

    hmm nice article……

  47. technology (4 comments)12 July 10

    Great stuff Angie. The way you have explained it here is easy to understand.
    Much appreciated.

  48. Thank you for the excellent visual reference and explaination of the WP hierarchy! I’ve been struggling with this for a while and finally stumbled on your blog.

  49. smith (2 comments)16 July 10

    I love to use Drupal Content Management system.I think in these days many companies and individuals try to develop their own blogs.so i recommend them Drupal is one of the best software.

  50. hongza (1 comments)16 July 10

    Shopping Online Electronics Computers Cameras http://www.shoppingsalelowestprices.com/index.php

  51. Nice article. It’s well written and clear, easy to understand. I’m sure it will help WP beginners. :)

  52. SFIhomebizz (1 comments)20 July 10

    A useful and informative article for wordpress users. Thankyou

  53. Nice, This is a great desctiprion of how the WordPress Hierarchy actually works. A good read for anyone that needs a little help with the Hierarchy

  54. Dave George (3 comments)29 July 10

    Very well structured post about WP hierarchy !

  55. Orfebre (1 comments)31 July 10

    I love to use Drupal Content Management system.I think in these days many companies and individuals try to develop their own blogs!!!

Leave a Comment

Your reply will be added to the comment above (Below any other replies to this comment) -

(We DoFollow)

Not sure how to get an image with your comment?