How WordPress Determines Which Template File To Use On Individual Pages?

Does this ever happen to you? You started a website on WordPress and then you start getting comfortable with the platform. And the story goes on like that for some time. Then, suddenly you want more control over your WordPress website. You start testing new plugins and themes. Started going through WordPress codex for additional information about the platform. Sounds familiar? If yes, you don’t have to worry. This is exactly why WordPress is made in the first place. And it is also natural because WordPress caters loads of features and tweaks which often falls as you wanting more control over your website. Are you looking for additional information about to alter your WordPress website design and functionality? If yes, you are exactly in the right place. In order to do that, you need to understand how WordPress algorithm determines which template file to use on individual pages.

Click Here to Hire Affordable WordPress Developer for your website!

That is exactly what I am going to disclose in this post. I will provide you an in-depth information about the WordPress template hierarchy. This information will cater you better assistance when you will tweak your WordPress website design. Let’s get you started with it,

What Do You Understand By Template?

You must have heard page template word before. They are basically the page layouts that define the representation of your content on a WordPress website. There are different template files for each page type present on your WordPress website. It may be for posts, web pages, archives, and attachments. Once you know the function of these templates, you can individualize each page’s appearance accordingly. This can give your website a husky and unique appearance. In order to do that, you must know the template hierarchy of the WordPress website to make order out of chaos.

What Is The Template Hierarchy?

There is no limit in WordPress when it comes to the amount of usage of your WordPress website pages in terms of categories. For instance, take an example of an archive page. Archive page may appear in the category of the archives or web page.

What’s the catch here:

The catch is quite simple. Now in the above case, if there is a template for both of the category, how WordPress will choose which one it will use? The WordPress Template Hierarchy. This is the factor that will come to play a definitive role here. Whenever a request is made by your website to WordPress, it will follow the template hierarchy and then cater the template which first matches the template file.

You might be wondering:

What if no match is found??? A fair question. There a possibility that there is no match for the template file. In that case, there is one template file which will play the pivotal role. It is the index.php file. It is the most important file without which no other WordPress file can exist. Whenever these kinds of situation arrive, it will turn towards the index.php file and use that to display the website content. Now, let’s move to the WordPress template hierarchy to understand the order of representation of the world’s best Content Management System.

WordPress Template Hierarchy: An Overview

No content hierarchy is same for all kinds of content present on the website. No matter if they all end up with the index.php template in the end. The middle step has always followed the hierarchy which is different for each type of content. So, let’s get you started with the template hierarchy of some of the major elements of the WordPress website. Here you go,

Template Hierarchy For Homepages

The hierarchy is quite simple here, 1. home.php 2. index.php If a file home.php is available in the database of your WordPress website, the template file will go for it before going to index.php. Yet there are many types of homepages which can cater different type of hierarchy. Let’s get some detailed template hierarchy of these homepages.

Front Page Hierarchy

If your homepage continuously displays upcoming posts and updated posts, then the template hierarchy will be like, 1. front-page.php 2. home.php 3. index.php If your homepage is the static one, then the hierarchy will be, 1. front-page.php 2. your_page-{slug}.php 3. your_page-{post-id}.php 4. page.php 5. index.php So, as you can see the homepage can be used in two different ways. Either to display regular dynamic homepage with latest posts or it can be a static homepage. For a front-page hierarchy, you can also refer to Nick Schaferhoff’s post in Smashing Magazine.

Hierarchy Of Archives & Author Archive Pages

For normal archives page, WordPress follows the following ladder, 1. archive.php 2. index.php Archive pages have a specific function to display all your selected posts for specific reason or group. For instance, you want that your archive page must display posts by specific author. When this situation arises, WordPress will go to the file name archive.php. If there is no archive file, then it will settle with the index.php file. As per the example above, if you are making a subcategory in the archive section, then WordPress will follow this hierarchy, 1. author-{author_name}.php 2. author-{author_id}.php 3. author.php 4. archive.php 5. index.php This system will specify your WordPress web page more aptly and a bit different from the normal archive page.

Hierarchy In Single Pages

Single pages tend to be static when we generally talk about them. What sort of hierarchy do they follow? Let’s have a look at this section of the post.

Single Post Hierarchy

The hierarchy which WordPress will follow is 1. single-{type_of_post_title}.php 2. single.php 3. index.php WordPress will go for single.php if the single-{type_of_post_title}.php is not present. Rest it will follow the same roadmap as others.

Get Started Now for WordPress Template Customization!

Web Pages Hierarchy

Web pages hold a special spot in WordPress template hierarchy system. As there is a possibility where you can assign your web page template from the WordPress editor. The web page hierarchy goes on like this, 1. the_custom_template 2. web_page-{slug}.php 3. web_page-{page_id}.php 4. page.php 5. index.php

Concluding Remarks

According to Jay Hoffman in Elegant Themes, the template hierarchy of WordPress is more than a reference to a website code. You can define every template separately if you want to seize the opportunity. All you need is more thorough knowledge about the theme customization via CSS and PHP files. Anyways, I hope you do understand the pathway used by WordPress to distinguish different page file type. The understanding of WordPress hierarchy is just the beginning of knowing the entire WordPress.