Understand The WordPress Template Hierarchy

Josh Pollock - March 28, 2014

http://www.chipbennett.net/themes/template-hierarchy/http://www.chipbennett.net/themes/template-hierarchy/

A WordPress theme is a collection of template files used for displaying content from the site’s database. How do you know which template will be used for what content? The answer to that question is the WordPress template hierarchy system.

A valid WordPress theme needs three files, “style.css”, “functions.php” and “index.php”. A theme with only these three files will use index.php for displaying all front-end content. Other template files can be added for different situations. For example, if a theme contains “single.php” it will be used for all single posts.

Resources For Learning The Template Hierarchy

The codex article on the template hierarchy is very well written and very readable. I highly recommend it as a starting point. I also recommend two charts that show what template is used in any situation. Both charts are read left to right. If a theme doesn’t have a template then WordPress falls back to the next template to the right, and so forth until arriving at index.php.

A good, easy to read, and simple way to visualize the system is Chip Bennet’s template hierarchy flow chart. Another option is the interactive template hierarchy chart by Rami Abraham and Michelle Schulp.

The Reveal Template Plugin

Mastering the template hierarchy takes time. As you learn you’re going to get confused and find yourself in many “which template is this?” type situations. To clear up your confusion, I strongly recommend using the plugin Reveal Template by Scott Reilly. This simple plugin appends the name of the template, or if you want, the full path to the template file to the footer or header of your page.

I strongly recommend, as a way to learn the template hierarchy that you leave this plugin active in your local development environment. Seeing the name of the template being used to display the current page, on every page is a great way driving home what you learn from the resources I listed above.

Quick Links