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:

  1. 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
  2. Determine which template file to use to display this page by template hierarchy.
  3. 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.phpOutside 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:

  1. 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.phpThe
  2. in the event that category-unicorns.phpdoes not exist and the category ID is 4, WordPress will look for a template file named category-4.php The
  3. in the event that category-4.phpdoes not exist, WordPress will look for a generic category template file that category.phpThe
  4. in the event that category.phpDoesn't exist, WordPress will be looking for generic archive templates  archive.phpThe
  5. in the event that archive.phpStill 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.

You also It is possible to interact with this chart The

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.phpThe

  1. home.php
  2. index.php

in the event that front-page.phpexists, it will override home.phpTemplate.

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.phpTemplate. If thefront-page.phpfile doesn't exist, WordPress will use the home.phpmaybe page.phpfile, depending on the settings in Settings → Reading. If neither of these files exists, end up using the index.phpDocumentation.

  1. front-page.php- Settings → Read Home Display set in the " Your latest posts "or" static page " This template file is preferred.
  2. 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. 
  3. page.php- exist Home Display Section Settings" Home "Time .
  4. index.php– Home Display Set to " Your latest post "but home.phpdoes not exist, or set theHomeDemocratic Republic of the Congo page.phpWhen 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.

  1. single-{post-type}-{slug}.php- (As of 4.4) First, WordPress looks for templates for specific posts. For example, if Article Type be productand the article alias isdmc-12WordPress will be looking for single-product-dmc-12.phpThe
  2. single-{post-type}.php- If the article type is product, WordPress will be looking for single-product.phpThe
  3. single.php- Then WordPress falls back to single.phpThe
  4. singular.php- And then it's back to singular.phpThe
  5. index.php- Finally, WordPress will fall back to the index.phpThe

single page

Template files for rendering static pages ( web page(Article Type). web pageis a special post type in WordPress, use the following path to find the template file:

  1. Customized template files- Selected in the WordPress backend Page templates . See get_page_templates()
  2. page-{slug}.php- If the page slug is recent-newsWordPress will use the page-recent-news.phpThe
  3. page-{id}.php- If the page ID is 6, WordPress will use the page-6.phpThe
  4. page.php
  5. singular.php
  6. index.php

categorized directory

WordPress uses the following hierarchy to render the category catalog pages.

  1. category-{slug}.php- If the alias of a categorized directory is newsWordPress will find category-news.phpThe
  2. category-{id}.php- If the category ID is 6WordPress will find category-6.php The
  3. category.php
  4. archive.php
  5. index.php

tab (of a window) (computing)

To display the tag archive page, WordPress uses the following path:

  1. tag-{slug}.php- If the tag's slug is sometagWordPress will find tag-sometag.phpThe
  2. tag-{id}.php- If the label's ID is 6WordPress will find tag-6.phpThe
  3. tag.php
  4. archive.php
  5. index.php

Customized Categories

WordPress FindCustomized TaxonomyThe template file path is approached slightly differently:

  1. taxonomy-{taxonomy}-{term}.php - If the taxonomy is sometaxand the taxonomy items are sometermIf you are a WordPress user, WordPress will look for the taxonomy-sometax-someterm.php.existArticle Formattingcase , the taxonomy is 'post_format' and the taxonomy item is 'post-format- {format}'. That is, the taxonomy-post_format-post-format-link.php Link format.
  2. taxonomy-{taxonomy}.php - If the taxonomy is sometax WordPress Find taxonomy-sometax.php The
  3. taxonomy.php
  4. archive.php
  5. index.php

Custom Article Types

Custom Article TypesUse the following path to render the appropriate archive page.

  1. archive-{post_type}.php- If the post type is product, WordPress will be looking for archive-product.phpThe
  2. archive.php
  3. 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:

  1. author-{nicename}.php - If the author's name is good mattWordPress will be looking for author-matt.phpThe
  2. author-{id}.php - If the author ID is 6WordPress will be looking for author-6.phpThe
  3. author.php
  4. archive.php
  5. index.php

dates

The date-based archive page follows the path below to find the template:

  1. date.php
  2. archive.php
  3. index.php

Search results

Search results follow the same pattern as other template types:

  1. search.php
  2. index.php

404 (not found)

Similarly, the 404 template file is called in the following order:

  1. 404.php
  2. index.php

attachment (email)

Present the attachments page (attachment(Article type) Use the following path:

  1. {MIME-type}.php -Can be any MIME type(Example: image.php (math.) genus video.php (math.) genus pdf.php ). For text/plain , use the following paths (in order):
    1. text-plain.php
    2. plain.php
    3. text.php
  2. attachment.php
  3. single-attachment-{slug}.php - For example, if the attached slug is holiday WordPress will look for single-attachment-holiday.php The
  4. single-attachment.php
  5. single.php
  6. singular.php
  7. 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:

  1. embed-{post-type}-{post_format}.php - First, WordPress finds templates for specific post types. For example, if a post has a post type of postand formatted as audio, WordPress will check for accuracy embed-post-audio.phpThe
  2. embed-{post-type}.php- If the article type is productWordPress will look for embed-product.php The
  3. embed.php- Finally WordPress will use embed.php The
  4. Finally, WordPress eventually falls back to the built-in wp-includes/theme-compat/embed.phpTemplate.

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 ::

  • 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' ); }

We offer WordPress Themes and Plugins Custom Development Services

This site has long undertaken WordPress themes, plugins, WooCommerce-based store mall development business. We have 10 years of experience in WordPress development, if you want to Developing Websites with WordPress, please contact WeChat: iwillhappy1314 or email: amos@wpcio.com for inquiries.

发表回复

Your email address will not be published. 必填项已用 * 标注

*