4R - WordPress Template Hierarchy System

The Template Hierarchy System

The template hierarchy system decides what template files will be used for every individual page on your website. In WordPress this heirarchy decides what PHP template file gets to be used to create a specific page on your website based on what the post content is. Whether it is displayed as a Page, Post or an archive of posts. In order for template heirarchy to work there must be an index.php template file otherwise the heirarchy will not work.

Template Hierarchy System - Order

WordPress uses a query string to determine which set or template set will be used to display the page. The query string is contained information that links to different parts of your site. Wordpress uses template hierarchy to find matching files. Therefore if WordPress can't find a template file with a name that matches then it will skip it and go to the next file in the hierarchy. Wordpress matches the query strings to query types to find out what page is needed. First is the Home Page or (index.php,home.php), WordPress sets the home page as a place to display your blog posts by default, also known as the blog posts index. Next is the Front Page Display, which is a template file that displays your websites home page, there are many different possible file names which include: front-page.php, home.php, page.php, or index.php. Next is the Single Post and Single Page template file that is used to show a single post or a static page. Categories, tags, custom post types and custom taxonomies are next and display on the given archive index page.

The Template Hierarchy System - File Naming Conventions

Each WordPress template follows a specific way of file naming. For the Home page WordPress will first look for front-page.php and use it to display the front page, next WordPress will search for home.php or index.php template to show the homepage if front-page.php is not there. For a single post WordPress first looks for a file named single-{post-type}-{slug}.php, single{post-type}-{slug} is used to change the display of a specific post in any of the post types. If single-{post-type}-{slug}.php is unavailable then a file named single-{post-type}.php will be checked by wordpress. If neither files are there then WordPress will use the default singple.php or singular.php and as a last resort index.php. For a single page WordPress first looks for a Custom Page Template that is assigned to the page. If that is unavailable then it will search for page-{slug}.php or page-{id}.php. Page-{id}.php is used when there is a page ID, if there is a page ID then WordPress will look for a file with the name of that ID, for example page-57.php. If none of those are created then the next files would be page.php, singular.php or lastly index.php.

Summary

The template hierarchy system is very important as it is making it easier for you to customize the appearance of your WordPress website. The WordPress template allows you to filter the hierarchy so you can adjust or add things in specific areas of the hierarchy.