WordPress Template Hierarchy

Template hierarchy system

WordPress uses query strings to decide on which template or set of templates should be used on a page. WordPress will search down through the hierarchy until it finds a matching template file and will default to the index.php if none are found.

Template hierarchy system - order

The order for the template hierarchy will depend on the type of page. For example, the Error 404 Page will search for 404.php, then go to index.php. The order is similar for the Search Result Page and Blog Post Index page. Singular and Archive pages have more complicated paths. A singular page will search for a Single Post Page or Static Page first then look for a custom, blog, or attachment post or Page Template. It goes on to search for custom or default templates. If a custom Template is found, it will then use that $custom.php, for example page-contact.php. Then it will go to single.php or page.php, if those don't exist it uses singular.php, and finally, index.php.

Template hierarchy system - file naming conventions

File naming conventions are used to define the type of page. For example page.php files will style pages but you can go a step further and create custom pages using page-custom.php or page_custom.php. A page_custom.php will create a template that admins can use to select a template when editing in the block editor. A page-custom.php will allow you to apply a template to a specifig page using the pages slug or id which will prevent admin users from altering the template in the editor.

Summary

WordPress offers a lot of flexibility in design by allowing multiple template options for categories of pages as well as specific pages. The template hierarchy ensures that you will have a functioning site withoutout creating files for each page by defaulting to the template for index.php. This hierarchy allows developers to customize as much or as little as they like and allows changes globally and individually.