WordPress Theme Development Tutorial Manual - Template File Inheritance Rules
As noted above. Template filesare modular, reusable files used to generate pages on a WordPress website. Some template files (such as header and footer templates) are used for all pages of your site, while others are used only under certain conditions.
This article introduces How WordPress determines which template files to use on each page . This will help us determine which template file we need to edit if we want to customize an existing WordPress theme. We can also use thecondition labelto control the templates that are loaded on the page.
Template File Hierarchy
summaries
WordPress Usagequery stringto determine which template should be used to display the page. The query string is the information contained in the links in each section of the site. It appears after the initial question mark and may contain a number of parameters separated by & symbols.
In short, WordPress searches down the template hierarchy until it finds a matching template file. To determine which template file to use, WordPress:
- Match each query string with the query type to determine what type of page the user is requesting (e.g., search page, category page, etc.); the
- Determine which template file to use to display this page by template hierarchy.
- Find a template file with a specific name in the current topic's directory and use the hierarchy to specify the First matching template file The
In addition to the basic index.php
Outside of the template files, we can customize the pages to be displayed using other template files.
If WordPress can't find a specific template file with a matching name, it will jump to the next file in the hierarchy. If WordPress still can't find any matching template files, it will eventually use theindex.php
template to display the page.
typical example
As an example, a user opens the classifieds archive page on your site:http://example.com/blog/category/your-cat/
, WordPress looks for template files in the current theme's directory that match the ID of the category in order to generate the correct page. More specifically, WordPress follows the following process:
- Find the template file that matches the category slug in the current theme's directory. If the category slug is "unicorns", WordPress looks for a template file named
category-unicorns.php
The - in the event that
category-unicorns.php
does not exist and the category ID is 4, WordPress will look for a template file namedcategory-4.php
The - in the event that
category-4.php
does not exist, WordPress will look for a generic category template file thatcategory.php
The - in the event that
category.php
Doesn't exist, WordPress will be looking for generic archive templatesarchive.php
The - in the event that
archive.php
Still missing, WordPress will use the theme index.php template.
Visualization Overview
The following figure shows the order in which the template files are called to generate pages in the WordPress template hierarchy.
Hierarchy Details
Although it's easier to understand through diagrams, we describe below in words the order in which the various queries call the template files.
Home Page Display
By default, WordPress displays the latest posts of the site on the front page of our site, which is called the post index. In addition to this, a static page is chosen as the home page of the site. Template Files home.php
Used to display an index of articles, whether we choose the latest articles or static pages for our home page. If home.php
does not exist, WordPress will use the index.php
The
home.php
index.php
in the event that front-page.php
exists, it will override home.php
Template.
Home Display
front-page.php
The template file is the front page template file that WordPress prioritizes finding. This template is prioritized over the home.php
Template. If thefront-page.php
file doesn't exist, WordPress will use the home.php
maybe page.php
file, depending on the settings in Settings → Reading. If neither of these files exists, end up using the index.php
Documentation.
front-page.php
- Settings → Read Home Display set in the " Your latest posts "or" static page " This template file is preferred.home.php
- If WordPress can't find front-page.php, and it's in the Home Display WordPress will look for home.php when "Your latest posts" is set, and also when a post page is set in the home display section.page.php
- exist Home Display Section Settings" Home "Time .index.php
– Home Display Set to " Your latest post "buthome.php
does not exist, or set theHomeDemocratic Republic of the Congopage.php
When not present, use this template file.
As we saw above, there are many paths for WordPress to determine templates, and using the chart above is the best way to determine which template file WordPress will use.
Single article page
Single article templates are used to display a particular article.
single-{post-type}-{slug}.php
- (As of 4.4) First, WordPress looks for templates for specific posts. For example, if Article Type beproduct
and the article alias isdmc-12
WordPress will be looking forsingle-product-dmc-12.php
Thesingle-{post-type}.php
- If the article type isproduct
, WordPress will be looking forsingle-product.php
Thesingle.php
- Then WordPress falls back tosingle.php
Thesingular.php
- And then it's back tosingular.php
Theindex.php
- Finally, WordPress will fall back to theindex.php
The
single page
Template files for rendering static pages ( web page
(Article Type). web page
is a special post type in WordPress, use the following path to find the template file:
Customized template files
- Selected in the WordPress backend Page templates . Seeget_page_templates()
page-{slug}.php
- If the page slug isrecent-news
WordPress will use thepage-recent-news.php
Thepage-{id}.php
- If the page ID is 6, WordPress will use thepage-6.php
Thepage.php
singular.php
index.php
categorized directory
WordPress uses the following hierarchy to render the category catalog pages.
category-{slug}.php
- If the alias of a categorized directory isnews
WordPress will findcategory-news.php
Thecategory-{id}.php
- If the category ID is6
WordPress will findcategory-6.php
Thecategory.php
archive.php
index.php
tab (of a window) (computing)
To display the tag archive page, WordPress uses the following path:
tag-{slug}.php
- If the tag's slug issometag
WordPress will findtag-sometag.php
Thetag-{id}.php
- If the label's ID is6
WordPress will findtag-6.php
Thetag.php
archive.php
index.php
Customized Categories
WordPress FindCustomized TaxonomyThe template file path is approached slightly differently:
taxonomy-{taxonomy}-{term}.php
- If the taxonomy issometax
and the taxonomy items aresometerm
If you are a WordPress user, WordPress will look for thetaxonomy-sometax-someterm.php.
existArticle Formattingcase , the taxonomy is 'post_format' and the taxonomy item is 'post-format- {format}'. That is, thetaxonomy-post_format-post-format-link.php
Link format.taxonomy-{taxonomy}.php
- If the taxonomy issometax
WordPress Findtaxonomy-sometax.php
Thetaxonomy.php
archive.php
index.php
Custom Article Types
Custom Article TypesUse the following path to render the appropriate archive page.
archive-{post_type}.php
- If the post type isproduct
, WordPress will be looking forarchive-product.php
Thearchive.php
index.php
(To render a single article type template, see the aboveIndividual article display(Partially.)
Author's display
Based on the above example, the hierarchy for presenting author archive pages is obvious:
author-{nicename}.php
- If the author's name is goodmatt
WordPress will be looking forauthor-matt.php
Theauthor-{id}.php
- If the author ID is6
WordPress will be looking forauthor-6.php
Theauthor.php
archive.php
index.php
dates
The date-based archive page follows the path below to find the template:
date.php
archive.php
index.php
Search results
Search results follow the same pattern as other template types:
search.php
index.php
404 (not found)
Similarly, the 404 template file is called in the following order:
404.php
index.php
attachment (email)
Present the attachments page (attachment
(Article type) Use the following path:
{MIME-type}.php
-Can be any MIME type(Example:image.php
(math.) genusvideo.php
(math.) genuspdf.php
). Fortext/plain
, use the following paths (in order):text-plain.php
plain.php
text.php
attachment.php
single-attachment-{slug}.php
- For example, if the attached slug isholiday
WordPress will look forsingle-attachment-holiday.php
Thesingle-attachment.php
single.php
singular.php
index.php
Embedded Functions
Embed template files are used to render posts that are embedded in other pages or websites. Starting with 4.5, WordPress uses the following path:
embed-{post-type}-{post_format}.php
- First, WordPress finds templates for specific post types. For example, if a post has a post type ofpost
and formatted as audio, WordPress will check for accuracyembed-post-audio.php
Theembed-{post-type}.php
- If the article type isproduct
WordPress will look forembed-product.php
Theembed.php
- Finally WordPress will useembed.php
The- Finally, WordPress eventually falls back to the built-in
wp-includes/theme-compat/embed.php
Template.
Handling of Non-ASCII Characters
Starting with WordPress 4.7, any dynamic part of a template name (whose name contains non-ASCII characters) actually supports both unencoded and encoded forms. We can choose to use either one.
Below is the page template hierarchy for a page named "Hello World 😀" with ID 6
::
page-hello-world-😀.php
page-hello-world-%f0%9f%98%80.php
page-6.php
page.php
singular.php
The same rules apply to post slugs, term names and author nicenames.
Filtering Hierarchy
The WordPress template system allows us to modify the template hierarchy using filters. This means we can insert and change content at specific points in the hierarchy. Filters (located in the get_query_template() function) using the filter "{$type}_template", where $type is the template type.
Below is a list of all the filters available in the template hierarchy:
embed_template
404_template
search_template
frontpage_template
home_template
taxonomy_template
attachment_template
single_template
page_template
singular_template
category_template
tag_template
author_template
date_template
archive_template
index_template
typical example
For example, the default author archive template hierarchy is as follows:
author-{nicename}.php
author-{id}.php
author.php
We want to be in author.php
Before adding a author-{role}.php
To set a specific template file for a role, we can use the 'author_template' template filter to modify the hierarchy of the author archive template. When a user opens the /author/username page, WordPress will automatically call the role archive template, where username is the 'editor' role, and this page will be displayed using author-editor.php (if it exists in the current theme directory).
function author_role_template( $templates = '' ) {
$author = get_queried_object();
$role = $author->roles[0];
if ( ! is_array( $templates ) && ! empty( $templates ) ) {
$templates = locate_template( array( "author-$role.php", $templates ), false );
} elseif ( empty( $templates ) ) {
$templates = locate_template( "author-$role.php", false );
} else {
$new_template = locate_template( array( "author-$role.php" ) );
if ( ! empty( $new_template ) ) {
array_unshift( $templates, $new_template );
}
}
return $templates; }
}
add_filter( 'author_template', 'author_role_template' ); }