In WordPress, the template hierarchy is the system that WordPress uses to determine which template file to use when displaying different types of content. This hierarchy is crucial for theme development as it defines how WordPress selects templates based on the URL or query being viewed.
When a user visits a page on a WordPress site, WordPress matches the URL with a specific type of content (such as a post, page, category, or archive). It then looks through the hierarchy to find the appropriate template file within the active theme to render that content.
-
Home Page
home.php
: Used to display the homepage if it's a blog index.front-page.php
: Used if a static front page is set in the settings.index.php
: The fallback template if no other template is available.
-
Single Post
single-{post-type}.php
: Used for custom post types (e.g.,single-product.php
for products).single.php
: Used for single posts of the default "post" type.singular.php
: A more general template used for any single post or page.index.php
: The fallback template.
-
Pages
page-{slug}.php
: Used for a specific page with a matching slug (e.g.,page-about.php
).page-{id}.php
: Used for a specific page with a matching ID (e.g.,page-10.php
).page.php
: Used for all pages.singular.php
: General template used for any single post or page.index.php
: The fallback template.
-
Category
category-{slug}.php
: Used for a specific category with a matching slug (e.g.,category-news.php
).category-{id}.php
: Used for a specific category with a matching ID (e.g.,category-5.php
).category.php
: Used for all category archives.archive.php
: Used for all archive pages.index.php
: The fallback template.
-
Tag
tag-{slug}.php
: Used for a specific tag with a matching slug (e.g.,tag-special.php
).tag-{id}.php
: Used for a specific tag with a matching ID.tag.php
: Used for all tag archives.archive.php
: Used for all archive pages.index.php
: The fallback template.
-
Custom Post Type Archive
archive-{post-type}.php
: Used for a specific custom post type archive (e.g.,archive-product.php
).archive.php
: Used for all archive pages.index.php
: The fallback template.
-
Author
author-{nicename}.php
: Used for a specific author with a matching nicename.author-{id}.php
: Used for a specific author with a matching ID.author.php
: Used for all author archives.archive.php
: Used for all archive pages.index.php
: The fallback template.
-
Date
date.php
: Used for date-based archives.archive.php
: Used for all archive pages.index.php
: The fallback template.
-
Search Results
search.php
: Used for search result pages.index.php
: The fallback template.
-
404 Error
404.php
: Used when no content is found (a "Page Not Found" error).index.php
: The fallback template.
Let's say you visit a URL that corresponds to a single post. Here's how WordPress will determine which template to use:
- Check for
single-{post-type}.php
: If the post is a custom post type (e.g., product), WordPress will look for a template namedsingle-product.php
. - Check for
single.php
: If no custom post type template is found, it will look forsingle.php
. - Check for
singular.php
: If neither of the above templates exists, WordPress will look forsingular.php
. - Fallback to
index.php
: If none of the specific templates are available, WordPress will use theindex.php
file.
Understanding the template hierarchy allows theme developers to create specific templates for different content types, offering greater flexibility and control over how content is displayed on a WordPress site. This hierarchy also makes it easier to create child themes by overriding specific templates while leaving the rest of the parent theme intact.
In summary, the WordPress template hierarchy is a powerful tool for customizing the appearance and structure of a WordPress site based on different types of content and requests.