WordPress Template Heirarchy System

Define/Explain Template Heirarchy System

The WordPress template heirarchy system is the way WordPress is designed to determine what template file is used when a file is requested. This system determines which file should be used when a template of that name is called for on a page. The template used is based on the name of the template that is called.

Template Heirarchy Order

The order in which WordPress looks for/uses a template is set by WordPress. It starts at the top by determining the main type of request (Is It an archive? single page? search result? etc.). From there it looks for the type of page within that section. Eventually WordPress determines if the request is for a custom template or a default template and chooses. If those fail, it will fall back to the default for that category (Example: Singular page falls to page.php and an archive page goes to archive.php). If no template by that name is found anywhere along the line, the default backup is to use index.php. Custom templates include the name of the template or the template id as part of the file name (described below).

File Naming Conventions

The name of the php file determines if it is chosen based on the request for a file. When WordPress is looking for a specific category template, like the category "gardening", it will look for the template category-gardening.php and if that isn't found it will look for the file with the category id in it (like category-2.php if the gardening category has an id of 2). As mentioned above, it will continue on to category.php > archive.php > index.php if the specific category templates aren't found. In order for your template to be found, it's important to name it specifically for what you are calling within the php file you create (or WordPress front end).

Summary of the Documentation

Knowing how to work with the template heirarchy system allows you to create custom layouts and code for your WordPress site to furthur customize how it looks and functions. It's easy to create a specific file to update the look of the site by simply naming it within the naming conventions for the heirarchy.

Sources