Template Hierarchy System

What is the Template Hierarchy System?

In WordPress, there is a template hierarchy system that is used to determine which type of page layout to use on certain pages based on the include files provided in the themes folder in WP-Content. For example, if only index.php was provided, every page would look like index.php, but if other files such as page.php, or single.php were also in the mix, they would take priority over index.php on certain pages. More specifically, single.php is used for a post page, while page.php is used for a standard, non-post page. These can get more specific by including page ids or slugs into the file names, or choosing a different name for the file all-together.

Order of Page Template Files

In the case of working with a page template file, there is a specific priority that WordPress will look for when building the page. To begin with, there are two types of templates, custom and default. Custom templates prioritize a php file named the specific template (full-width.php). Default templates follow a different route, where the slug of the page is prioritized first., resulting in page-$slug.php . If no php file is found using a slug, then the page id is prioritized next, resulting in page-$id.php .

File Naming Conventions

When naming these PHP files, it is important to understand that custom templates and default templates are completely separate, and only one can be used on a page at a time. The $slug value in page-$slug.php is the name of the specific page the file will be used for, for example, page-company.php is used on a page that has a slug of 'company'. Each page has an ID, and the ID can be found in the address bar when working on a specific page in WordPress, or can be found as a class name on certain HTML elements when inspecting a WordPress Page. The resulting php file could look something like page-42.php. Using the $slug php file could be risky, as slugs can be changed significantly easier than a page ID. A page ID is something that can't be changed, unless that specific file is deleted, and the WordPress system decides to use that same ID on a newly created page.

Summary

The WordPress Template Hierarchy System is an efficient way to prioritize the use of templates for specific pages, that gradually broadens out until index.php is used. In regards to template files, custom and default templates exist; custom template files are named directly after the template name (full-screen.php), while default templates have an identifier, the page slug or id, on the end of the file name to specify a file.