A Detailed Explanation of the WordPress Template Hierarchy

WordPress template hierarchy

In a word, Template Hierarchy is the basic rules of WordPress theme generation. If you have a Basic understanding of Template Hierarchy, you can read the full article to view the Template hierarchy with Example and Diagram.

How WordPress display Homepage:

The homepage is the first page of a blog or website. Usually, the URL of the homepage is like this https://www.yoursite.com. WordPress has a specialized template for home.php to display the homepage. If home.php is missing then WordPress generates the homepage using Index.php.

Note: if you choose static FrontPage then WordPress gives priority on a FrontPage template as the site homepage. There are 2 types of template file can be implemented as the template hierarchy to display Home Page.

The lists of the home page template files are described below with example:

  1. WordPress first looking for a template name home.php to generate the home page
  2. If home.php is missing WordPress will use the index.php to create a home page.

How WordPress displays a Homepage:

WordPress is very flexible to control the Homepage Display. You can control it by going to the front page display section of the Reading tab under the settings tab from the WordPress dashboard. There are two options for this part; the first choice is displaying the Homepage and the second choice is displaying the Post feeds. If you select the static front page for your blog, then WordPress will follow the page display rules. And your homepage is displayed as a page. But if you want to show the Post Page as a front-page, then WordPress will use the blog feed or archive page as a homepage.

There are three types of template file can be implemented as the template hierarchy to display Front Page.

The lists of the front page template files are described below with example:

  1. front-page.php
  2. If you want to display a front page as a WordPress page, then you can follow the Page displaying guidelines for WordPress.
  3. And, if you want to display the post page or blog feed as a homepage then follow the Archive page displaying guideline for WordPress

How does WordPress displaying a Single Post Page:

WordPress uses a single post page to present a full post or article. The default template for a single post page is single.php if single-{post_type}.php does not exist in the theme directory. However, If WordPress doesn’t find a single.php in the WordPress directory, then it uses the index.php to generate a single post page.

There is three template file can be implemented as the template hierarchy to display Single Post Page.

The lists of the single post page template files are described below for example:

  1. For displaying a single post page WordPress first looking for the template named single-{post_type}.php. For instance, if you want to show some software as a post then WordPress searching a template in the theme’s root directory called single-software.php.
  2. If WordPress doesn’t find a template named single-software.php then it presented the post result on single.php
  3. If single.php is missing, then the results are generated by index.php

How WordPress display a page:

WordPress check first if the page is switching to a custom template file. Then looking for the template files described below.

There is five template file can be implemented as the template hierarchy to display a page.The lists of the page template files are described below with example:

  1. To show a page, WordPress first searching for a custom template file. If this file is not present, then WordPress generated the page on page-{slug}.php.
  2. For example, suppose you create a page named my sweet home. If you have not any other post with the same name, then your slug should be my-sweet-home. If you choose pretty permalink, then your slug showed in your URL like this http://www.yoursite.com/my-sweet-home/ and now WordPress looking for a template named page-my-sweet-home.php.
  3. If WordPress doesn’t find any template file as the name of page slug then it will be looking for a template named page-{id}.php. For example, if the page id is 10 then WordPress looking for a template called page-10.php.
  4. If page-{id}.php is missing then WordPress generated the page on page.php
  5. If page.php does not exist then WordPress use index.php

How WordPress display a category page:

WordPress follow the flowchart for showing a category archive page. There is five template file can be implemented as the template hierarchy by indicating a category page.

The lists of the category page template files are described below with example:

  1. To displaying a category page WordPress first looking for the template as the name of the category slug (category-{slug}.php). For example, if your category name is “Fashion” then WordPress will be looking for a template named category-fashion.php
  2. If the first file is missing, then WordPress search for a file as the numeric id of your category (category-{id}.php). For example, if your category id is 10 then WordPress looking for a template file named category-10.php
  3. If the template file is missing as the category id, then WordPress generated the category page on the main category template named category.php
  4. If category.php does not exist, then WordPress uses archive.php
  5. If archive.php is missing, then WordPress use the main template file called index.php

How WordPress display a tag page:

To show a tag archive page WordPress follow the rules as described in the flowchart. There is five template file can be implemented as the template hierarchy to display Single Post Page.

The lists of the tag page template files are described below with example:

  1. To display a tag page WordPress first looking for a template named tag-{slug}.php. For example, if your tag is iPhone then WordPress search a template named tag-iphone.php
  2. If the file tag-{slug}.php doesn’t exist then WordPress looking for a file named tag-{id}.php. If your tag id is 10, then WordPress looks for a template named tag-10.php.
  3. If the id template is missing, then WordPress uses the main tag template named tag.php
  4. If tag.php does not exist then, WordPress uses archive.php
  5. If archive.php doesn’t exist then WordPress uses index.php

How WordPress display a custom Taxonomy page:

There is five template file can be implemented as the template hierarchy to show a custom Taxonomy page.

The lists of the taxonomy page template files are described below for example:

  1. For displaying a custom taxonomy page result, WordPress firstly use taxonomy-{taxonomy}-{term}.php. For example, if your taxonomy is the taxonomy-post format and the term is post-format-link then you template file name should be taxonomy-post_format-post-format-link.php.
  2. If this file is missing then WordPress looking for a template file named taxonomy-{taxonomy}.php. For example, if your taxonomy is some-tax your file name should be taxonomy-some-tax.php
  3. If the file taxonomy-{taxonomy}.php is missing then WordPress looking for the main taxonomy template called taxonomy.php
  4. If main taxonomy template is missing then WordPress looking for the main archive template named archive.php
  5. If archive.php is missing, then WordPress generated a taxonomy page result on index.php

How WordPress display an archive page:

There is three template file can be implemented as the template hierarchy to show a custom post page.

The lists of the archive page template files are described below for example:

  1. To generate an archive page WordPress first looking for a file named archive-{post_type}.php. For example, if your archive post type is product then WordPress looking for a template file named archive-product.php
  2. If archive-{post_type}.php is missing then WordPress look for main template file named archive.php
  3. If archive.php is still missing, then WordPress generated an archive page on index.php

How WordPress display an author page:

There is five template file can be implemented as the template hierarchy to display a custom post page.

The lists of the author page template files are described below for example:

  1. First WordPress is looking for a name as the author nicename or username. For instance, if author name is mahbuba than author-{nicename}.php should be like author-mahbuba.php.
  2. If wordpress doesn’t find author-{nicename}.php then it is looking for author-{id}.php (e.g. author-10.php) to generate a author page result.
  3. If author-{id}.php is missing, then WordPress use main author template named author.php
  4. If author.php doesn’t exist, then WordPress uses archive.php to display an author page.
  5. If archive.php does not found, then WordPress uses main template file to generate index.php

How WordPress display a date page:

There is three template file can be implemented as the template hierarchy to show a date archive page.

The lists of the calendar page template files are described below for example:

  1. To generate a calendar page WordPress first looking for a template file named date.php
  2. If date.php is missing, then WordPress use archive.php
  3. If archive.php doesn’t exist, then WordPress use index.php

How WordPress displays a search result page:

There is two template file can be used as the template hierarchy to show a search result page.

The lists of the search template files are described below with example:

  1. Everyone knows that what search result is. WordPress uses a custom template file to generate search page named search.php and looking for this template file first when a search is queried.
  2. If seaech.php is missing, then search result is generated by index.php

How WordPress display a 404 (Not found) result page:

There is two template file can be implemented as the template hierarchy to show a 404 (Not found) page.

The lists of the 404(not found) template files are described below with example:

  1. When people search with a keyword, but the topics do not exist on the website then the result of a not found page display on 404.php.
  2. If 404.php is missing then wordpress usemain tamplate file to generated 404(not found page) on index.php

How WordPress display a Attachment file page:

There are 5 type of template file can be implement as the template hierarchy to Attachment file page.The lists of the attachment template files are describes below with example:

  1. MIME_type.php (image.php, video.php, application.php) For text or plain it should be (text.php, plain.php, text_plain.php)
  2. If MIME_type.php is missing then WordPress use single-attachment.php
  3. WordPress use attachment.php if single-attachment.php is missing.
  4. If attachment.php doesn’t exist then WordPress uses single.php
  5. index.php use to display an attachment page if single.php is missing.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.