WordPress Template Hierarchy

WordPress is one of the best and popular Content Management System. It comes with powerful templating capabilities which provide immense control over content presentation and layout.

WordPress allows you to manipulate files for creating complex themes which enable you to present different types of content using different layouts and visual options.

In this article, we will describe WordPress Template Hierarchy, how it functions, and how it affects your blog.

So, let’s go down straight to learn more about WordPress Template Hierarchy.

WordPress Template Hierarchy

What is WordPress Template Hierarchy?

To understand WordPress Template Hierarchy, it is first necessary to understand how WordPress renders pages.

Let’s assume that you have a WordPress blog named, myblog.com.

What happens when you access a certain page on your blog?

Suppose you want to access a page with the URL:
http://myblog.com/category/my-posts/

When you key in this URL into your browser, the first thing that WordPress would do is to determine what type of page is being requested.

As a matter of fact, WordPress has different pages for different content types such as a home page, an archive page, a search page, etc.

Once WordPress ascertains the page-type, it goes on to find the template for that particular page.

In this example, you are requesting my-posts category page. So, WordPress will locate the template for this specific category.

Let’s describe the process in individual steps:

  1. WordPress will begin by searching for a template for the category (here: my-posts). As a matter of fact, each category is associated with a particular ID. Taking the above example, if the category ID is 6, WordPress will search for the template file named category-6.php.
  2. If the category-6.php file is not located, WordPress will move up one level and use the category.php template for rendering the page.
  3. If the category.php template is not available, WordPress will use the archive.php template.
  4. If archive.php is not available, WordPress will use the default index.php template.

So, you see that the progression is from specific to general templates.

The flow for the above example can be represented as follows:

Category-6.php >> category.php >> archive.php >> index.php

As a matter of fact, WordPress follows different hierarchies for different page types.

Let’s take another example to illustrate this.

Suppose you have your website front page at myblog.com, then WordPress will take the following path to ascertain which template it should be using:

  1. WordPress will first look for the front-page.php template.
  2. If front-page.php is not available, it will look for the template to the next level – that is – home.php.
  3. If home.php is also not available, WordPress will use the default index.php.

So, you see that WordPress resorts to using index.php, if any other template matching the content type is not available. Thus, by using the index.php, you could run a fully functional WordPress website.

WordPress Template HierarchyImage Source

WordPress Template Hierarchies by Content Type

Hereunder, we have shown WordPress Template Hierarchies for certain content types:

Home Pages

WordPress uses the index.php file to render your home page. As a matter of fact, the index.php file is used when there is no other template file specified for a page. It is an essential file. Front-page.php also affects the homepage of your site. If you include this file in your theme, it will override index.php, and takes priority when a user requests a home page. It is used only on your site’s homepage.

So the template hierarchy for the homepage of your site looks like this:

Front-page.php >> index.php

Category Pages

Category Pages are a special type of archive. WordPress uses the following hierarchy when showing up category pages.

  1. WordPress will first use a specific category name (my-posts.php).
  2. Each category is associated with a category ID. WordPress automatically creates it. It will use the category ID next, say category-6.php.
  3. If the category ID template is not available, the default category.php template is used.
  4. If the category template is missing, then the basic archive.php template is used.
  5. If even archive.php template is missing, then the index.php template is used.

The template path is as follows:

Category-slug.php -> category-ID.php -> category.php -> archive.php -> index.php

Author Pages

For displaying Author Pages, WordPress takes the following path:

  1. The first template that is searched is the author name like author-edward.php
  2. If the author name template is not available, WordPress will look for author ID Template. Like category ID, author ID is also automatically generated by WordPress.
  3. If the author-id.php template is not available, WordPress will look for a generic author.php template.
  4. If the author.php template is missing, then the basic archive.php template is used.
  5. Lastly, the index.php template is used if even the archive.php template is missing.

It can be shown as follows:

BlogAuthor-name.php -> BlogAuthor-id.php -> BlogAuthor.php -> archive.php -> index.php

Other Archive Pages

Now, you have a fair bit of idea of how archive pages work in the template hierarchy. The template hierarchy is followed for any taxonomy in WordPress. If you want to create a template file for tag pages, you can do it by creating a file called tag.php or even tag-id.php. For displaying date pages, you can create a file called date.php. If you want to create a custom for a specific custom post type, you can use the naming convention as archive-[post_type_name].php.

Other Templates

The template file 404.php is displayed when a requested page is not found, or a user mistyped the URL. The template file search.php is used to display the search results. Sometimes they are handled by the index.php file of your theme.

Single Posts and Pages

Here, we will look at what type of template hierarchy is followed by WordPress for displaying single pages and individual blog posts.

Let us see the template hierarchy for standard single blog posts:

  1. WordPress will first look for the single-post.php template.
  2. If it is not available, WordPress will look for the generic single.php template.
  3. Lastly, WordPress will use index.php if even single.php is missing.

The path can be represented as follows:

Single-post.php >> single.php >> index.php

If you have a custom post type, then WordPress first looks for a custom template such as single-posttype.php. The path can be represented as follows:

Single-posttype.php >> single.php >> index.php

For single static pages, the template hierarchy is slightly different:

  1. WordPress will first look for the custom template for the static page. It is custom.php.
  2. If the default template is used, it will use the page-slug.php template.
  3. Next, it will use the page-id.php template.
  4. If this is not available, it will look for the page.php template.
  5. Lastly, the index.php template is used.

This can be shown as follows:

custom.php -> page-slug.php -> page-id.php -> page.php -> index.php

Concluding

So, you see that WordPress allows you to create custom templates for displaying various content types. WordPress template hierarchy is followed by the visual presentation of different content types. It provides a great degree of control over your blog’s presentation.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top