The Template Hierarchy System
When it comes to choosing what template WordPress will use to display the content, it will refer to the query string, which is a sequence of codes within a URI, or Uniform Resource Identifier. WordPress will search for a file that matches the query string within the template hierarchy system. Once it finds one, it will display the content based on the code within that file. If WordPress is unable to find a file that matches the query string, it will display index.php since index.php will always exist.
The Order of the Template Hierarchy System
The template hierarchy order begins with seeking what type of page file is being requested, selecting from: archive page, singular page, site front page, blog posts index page, error 404 page, or search results page. An example of what happens from there is if WordPress is looking for a singular page, WordPress then sees if the query is looking for a single post page or a static page. If the query is seeking a static page, WordPress then looks for a custom template file or a default template file (a specialized template file). If WordPress is seeking a custom template file, the $custom.php file will be used. However, is there is no $custom.php file, then WordPress will see if there is a default template file, such as page-slug.php first, then page-id.php if page-slug.php does not exist. If neither are found, page.php is loaded, if page.php is not found, then singular.php is loaded, if singular.php is not found, then index.php is loaded. Ultimately the index.php file will always exist and will be the last file used if no other file matching the query string is found. If a default, or specialized, template file is being searched for instead of a custom template file, WordPress will not search for a $custom.php file, and instead search directly for page-slug.php first, then page-id.php next. If neither are found, WordPress continues the same sequence of file searching as already explained.
The resource, developer.wordpress.org, provides all of the pathways that WordPress might journey down based on a query string.
File Naming Conventions of the Template Hierarchy System
The naming conventions of the WordPress template hierarchy system provides insight into what part of the WordPress website will be impacted or displayed. For example, when loading a static page, WordPress will look for files such as $custom.php, which loads a file that displays the template for a custom template. A custom template is a template an admin can select to design a particular page on a WordPress website. WordPress may also be seeking a file named page-slug.php, which will load a file that has been assigned to a certain slug by an admin user. A slug is one way to identify a singular page or post. Similarly, WordPress may instead be seeking a file based on the ID of a page or post and is then seeking a page-id.php file. The ID is automatically assigned to a single page or post. If the slug or ID cannot be found, then WordPress will load the following files: page.php (if found), singular.php (if found), or index.php (will exist), in that order. The file, page.php, is a template used for all static pages. The file, singular.php, is a template used for all single post-type files. Lastly, the index.php file is the default fallback template.
Summary
In summary, the WordPress template hierarchy system provides templates based on meaningful and specific file names. This can then help a developer design the various parts of a WordPress website based on providing the desired code within the specific file based on its name. This ultimately provides more control over the user interface (UI) and user experience (UX) of a WordPress website.