[WORDPRESS series] Detailed introduction of WordPress template hierarchy

Reference source: http://blog.wpjam.com/article/wordpress-template-hierarchy/

The definition of WordPress template is very convenient, but before customizing the template, we need to know the template level of WordPress and how WordPress loads the template file , which will help rationally utilize resources and improve work efficiency when developing templates.

All-purpose index.php

We know that a simplest WordPress theme only needs style.css and index.php files. This means that index.php can be the template of any page, whether it is the home page, the article page, the category page, or the search result page, it can be perfectly competent.

That is to say, when the corresponding template does not exist, index.php will be the last card of WordPress.

Template loading

sequence Let's take a look at how WordPress loads templates. First,

WordPress will check whether there is a static home page. If there is a static home page, it will load the page content and display it through the front-page.php template file. If

Without this template file, it will be displayed through the page template.
If there is no static home page, the home.php template will be loaded.
If there is no home.php, the index.php template will be loaded.
Article page

The first thing to consider is single-{post_type}.php If such a template file (if the article type is product, the corresponding file name is single-product.php)
, if it is not found, the single.php template will be loaded
If there is no single.php template, load the index.php template.
Page The

first thing to consider is the custom template, which is the template selected when editing the page.
If there is no custom template, look for the page-{slug}.php alias template (for example, the page alias is about, and the corresponding file name is page-about .php )
If there is no alias template, look for the page-{id}.php ID template (for example, if the page ID is 2, the corresponding file name is page-2.php).
If there is no ID template, it is the
page.php If there is no page.php template, load the index.php template
Category page

The first thing to consider is the category-{slug}.php alias template (if the category alias is news, the corresponding file name is category-news.php)
If there is no alias template, then Find the category-{id}.php ID template (if the category ID is 1, the corresponding file name is category-1.php)
If there is no ID template, it is the category.php category template.
If there is no category.php category template, load it archive.php Universal archive template
If there is no archive.php universal archive template, load the index.php template
Tab page The loading order of the

tab page template is the same as that of the category page, except that the prefix is ​​not category but tag (such as tag-{slug}. php).

custom taxonomy

Sometimes we need other classification methods in addition to the default WordPress categories and tags. At this time, we will register a new taxonomy, so it is also necessary to know the template loading order of the taxonomy before this. The following instructions assume that the custom taxonomy is the name people:

The taxonomy taxonomy page is first considered as the taxonomy-{taxonomy}-{term}.php alias template (for example, the corresponding file name of the taxonomy alias teacher is taxonomy-people-teacher. php )
If there is no alias template, load the taxonomy archive page (eg taxonomy-people.php)
If there is no taxonomy archive page template, load the archive.php universal archive template
If there is no archive.php universal archive template, load it index.php
Template Custom Post Types

In addition to custom taxonomies, sometimes we also need custom post types:

custom post type archive pages are first considered archive-{post_type}.php alias templates (for example, the post type is product Then the corresponding file name is archive-product.php)
If there is no alias template, load archive.php Universal archive template
If there is no archive.php universal archive template, load index.php template
Author archive page The

first thing to consider is author- {nicename}.php alias template (if the author's username is steven, the corresponding file name is author-steven.php)
If there is no alias template, load the author-{id}.php ID template (if the author ID is 1, the corresponding file name is author-1.php)
If there is no ID template, load author.php Universal author template
If there is no universal author template then load the archive.php generic archive template
if there is no archive.php generic archive template then load the index.php template
date archive page the date.php date archive template is

first considered
if the date archive template is not available then load the archive.php generic archive Templates load index.php template
if archive.php generic archive template is not available
Search results page is

first considered search.php search results template
if no search results template is loaded index.php template
Attachment pages are loaded

first by MIME type Templates (such as image.php, video.php, application.php, and text/plain types try to load text.php, plain.php, text_plain.php in sequence.
If there is no corresponding MIME type template , attach .php attachment template (if there is no attachment.php, load single-attachment.php)
If there is no attachment template, load single.php article page template
If there is no single.php article page template, load index.php template
Template Hierarchy

The above text describes the loading order of WordPress templates in detail. Although detailed, it is not as intuitive as an infographic. The following is a template hierarchy diagram of WordPress.

WordPress Template Hierarchy Diagram

Using Hooks to Modify Templates

In some usage scenarios (such as plugins), we cannot directly modify template files. At this time, we can use hooks to modify template files. The hook name is in the format of {$type}_template. The following is a full list of corresponding hook names:

index_template
404_template
archive_template
author_template
category_template
tag_template
taxonomy_template
date_template
home_template
front_page_template
page_template
paged_template
search_template
single_template
text_template, plain_template, text_plain_template (all mime types)
attachment_template
comments_popup
template):

function page_template_filter( $templates='' ){
$templates=locate_template("single.php",false);
return $templates; }
add_filter
( 'page_template', 'page_template_filter' ); In the template of WordPress, we can clearly know how to output specific styles and content, but in some general templates (such as header.php), if we want to know which page template the user is currently visiting, we need to use the built-in conditional judgment function of WordPress. , these functions can help us determine what page is currently in order to facilitate loading different template content, such as is_home() is the conditional label for judging the home page, is_single() is the conditional label for judging the article page, and so on. Original link: http://www.dmeng.net/wordpress-template-hierarchy.html




Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327018661&siteId=291194637