Understanding of Wordpress Template Hierarchy

Understanding of Wordpress Template Hierarchy

Template files in WordPress are like building blocks, used to create web pages. Some, like headers and footers, are universal, while others are specific to certain pages. This guide details…

Template files in WordPress are like building blocks, used to create web pages. Some, like headers and footers, are universal, while others are specific to certain pages.

This guide details how WordPress selects the right template for each page. Knowing this helps when customizing a theme, guiding you on which template to modify.

The Template File Hierarchy

WordPress utilizes the query string in links to determine the appropriate template for displaying each page.

In simple terms, WordPress follows a hierarchy, scanning through templates until it finds the right match:

  1. It identifies the page type based on the query string (e.g., search, category, etc.).
  2. Then, it follows a predefined order in selecting the template.
  3. It searches for specific template files in the current theme’s directory, using the first matching template as per the hierarchy.

Except for the fundamental index.php template, you have the liberty to choose which templates to apply or omit.

When WordPress fails to locate a template file with a matching name, it moves on to the next file in the hierarchy. If no matching template is found, WordPress resorts to using the theme’s index.php file as a default fallback.

In a child theme, any file added will take precedence over the same file in the parent theme. For instance:

If both themes have ‘category.php,’ the child theme’s version is used.
If the child theme has a more specific file like ‘category-kenwey.php’ while the parent has a less specific ‘category.php,’ the child theme’s ‘category-kenwey.php’ takes priority.
However, if the child theme contains a general ‘category.php’ and the parent has a more specific ‘category-kenwey.php,’ the parent’s specific template, ‘category-kenwey.php,’ is used.

Visual Overview

The visual representation below illustrates the sequence of template files summoned to generate a WordPress page according to the WordPress template hierarchy:

Image credits from wordpress.org

Front Page display

The default setup in WordPress showcases your site’s home page with the latest blog posts. This page, termed the blog posts index, can alternatively display on a separate static page. The template file home.php is responsible for rendering this blog posts index, whether it functions as the front page or on a separate static page. If the home.php file is absent, WordPress resorts to using the index.php file.

Single Post

The single post template file is responsible for displaying a single post in WordPress, using the following path hierarchy:

  • single-{post-type}-{slug}.php (Introduced since version 4.4): Initially, WordPress seeks a template designed for the specific post. For instance, if the post type is ‘product’ and the post slug is ‘dmc-12,’ WordPress will search for a file named single-product-dmc-12.php.
  • single-{post-type}.php: If the post type is ‘product,’ WordPress looks for a file named single-product.php.
  • single.php: If specific post-type templates are not found, WordPress resorts to single.php.
  • singular.php: Following this, it defaults to singular.php.
  • index.php: Ultimately, if none of the above specific templates are present, WordPress falls back to the index.php file to render the single post.

Single Page

The template file used to display a static page (page post-type) in WordPress follows this specific path:

  • Custom Template File: This refers to the page template assigned to the page, which can be explored using the function ‘get_page_templates()’.
  • page-{slug}.php: If the page’s slug is ‘recent-news’, WordPress searches for a file named page-recent-news.php to display this specific page.
  • page-{id}.php: If the page’s ID is 6, WordPress will look for a file named page-6.php to render this particular page.
  • page.php: If no specific page template is found, WordPress defaults to page.php.
  • singular.php: Then, it moves to singular.php.
  • index.php: Ultimately, if none of the above specific templates exist, WordPress defaults to index.php to display the static page.

Category

WordPress follows this sequence to render category archive index pages:

  • category-{slug}.php: When the category’s slug is, for instance, ‘news,’ WordPress seeks a file named category-news.php for displaying that specific category.
  • category-{id}.php: In the case where the category’s ID is 6, WordPress looks for a file named category-6.php to display this specific category.
  • category.php: If no specific category template is found, WordPress defaults to category.php.
  • archive.php: If none of the above category-specific templates are available, WordPress defaults to archive.php.
  • index.php: Ultimately, if no specific category template exists, WordPress uses index.php for displaying the category archive index page.

Custom Taxonomies

Custom taxonomies follow this template file path for display:

  • taxonomy-{taxonomy}-{term}.php: If the taxonomy is labeled ‘sometax’ and the specific term is ‘someterm,’ WordPress searches for the file named taxonomy-sometax-someterm.php. In the case of post formats, the taxonomy is ‘post_format,’ and terms use the format ‘post-format-{format}’, like taxonomy-post_format-post-format-link.php for the link post format.
  • taxonomy-{taxonomy}.php: If the taxonomy is ‘sometax,’ WordPress looks for the file named taxonomy-sometax.php.
  • taxonomy.php
  • archive.php
  • index.php

Custom Post Types

Custom Post Types follow this sequence to display the suitable archive index page:

  • archive-{post_type}.php: WordPress searches for this file based on the post type. For example, if the post type is ‘product,’ it looks for archive-product.php.
  • archive.php
  • index.php

For rendering a single post type template, the process aligns with the single post display section described previously.

In Conclusion

Understanding the WordPress Template Hierarchy is crucial for effectively customizing and controlling the appearance of a WordPress website. It functions as a roadmap, guiding the system in selecting the most appropriate template file for each specific page or content type based on a predetermined order of priority.

Key takeaways include:

  1. Hierarchy Structure: The hierarchy is a structured sequence that WordPress follows to determine the most suitable template for rendering pages.
  2. File Search Order: WordPress systematically searches for specific template files based on criteria such as page types, slugs, and IDs, following a priority order.
  3. Customization Flexibility: Users can override default templates by employing more specific ones in a child theme, allowing for tailored design and functionality changes.
  4. Fallback System: If no specific template is available, WordPress falls back to more generic templates until it reaches the default index.php.

Comprehending this hierarchy aids in identifying which templates to modify or create for implementing desired changes or new page types. Ultimately, a robust grasp of the WordPress Template Hierarchy is a fundamental skill for effective WordPress theme customization and development.

In This Article

    Open chat
    Looking for Website Consultation ?
    Looking for Website Consultation Services? Let Me Help You Enhance Your Website