WordPress 中文文档
Template Hierarchy
出自WordPress Chinese 中文文档
wordpress.org.cn
div style=border: 1px solid #036; background:#eee; text-align:center; margin:5px; padding:10pxNote: This article covers an advanced topic. See Stepping Into Templates for an introduction to WordPress templates./div
目录 |
Introduction
WordPress Templates fit together like the pieces of a puzzle to generate the web pages on your WordPress site. Some templates (the header and footer template files for example) are used on all the web pages, while others are used only under specific conditions.
What this article is about
This article seeks to answer the following question:
blockquote Which template file(s) will WordPress use when it displays a certain type of page? /blockquote
Who might find this useful
Since the introduction of Themes in WordPress v1.5, Templates have become more and more configurable. In order to develop WP themes, a proper understanding of the way WordPress selects template files to display the various pages on your blog is essential. If you seek to customize an existing WordPress theme, this article aims to help you decide which template file needs editing.
Using Conditional TagsWordPress provides more than one way to match templates to query types. WordPress Theme developers can also use Conditional Tags to control which templates will be used to generate a certain page. Some WordPress Themes may not implement all of the template files described here. Some Themes use conditional tags to load other template files. See the Conditional Tags page and Query Based in Theme Development for more. |
The Template File Hierarchy
The General Idea
WordPress uses the Query String mdash; information contained within each link on your web site mdash; to decide which template or set of templates will be used to display the page.
First, WordPress matches every Query String to query types mdash; i.e. it decides what type of page (a search page, a category page, the home page etc.) is being requested.
Templates are then chosen mdash; and web page content is generated mdash; in the order suggested by the WordPress Template hierarchy, depending upon what templates are available in a particular WordPress Theme.
WordPress looks for template files with specific names in the current Theme's directory and uses the first matching template file listed under the appropriate query section below.
With the exception of the basic tt style=font-weight:bold; color:#036index.php/tt template file, Theme developers can choose whether they want to implement a particular template file or not. If WordPress cannot find a template file with a matching name, it skips down to the next file name in the hierarchy. If WordPress cannot find any matching template file, tt style=font-weight:bold; color:#036index.php/tt (the Theme's home page template file) will be used.
Examples
If your blog is at tt style=font-weight:bold; color:#036nowikihttp://example.com/wp//nowiki/tt and a visitor clicks on a link to a category page like tt style=font-weight:bold; color:#036nowikihttp://example.com/wp/category/your-cat//nowiki/tt, WordPress looks for a template file in the current Theme's directory that matches the category's ID. If the category's ID is 4, WordPress looks for a template file named tt style=font-weight:bold; color:#036category-4.php/tt. If it is missing, WordPress next looks for a generic category template file, tt style=font-weight:bold; color:#036category.php/tt. If this file does not exist either, WordPress looks for a generic archive template, tt style=font-weight:bold; color:#036archive.php/tt. If it is missing as well, WordPress falls back on the main Theme template file, tt style=font-weight:bold; color:#036index.php/tt.
If a visitor goes to your home page at tt style=font-weight:bold; color:#036nowikihttp://example.com/wp//nowiki/tt, WordPress first determines whether it has a static front page. If a static front page has been set, then WordPress loads that page according to the page template hierarchy. If a static front page has not been set, then WordPress looks for a template file called tt style=font-weight:bold; color:#036home.php/tt and uses it to generate the requested page. If tt style=font-weight:bold; color:#036home.php/tt is missing, WordPress looks for a file called tt style=font-weight:bold; color:#036index.php/tt in the active theme's directory, and uses that template to generate the page.
Visual Overview
The following diagram shows which template files are called to generate a WordPress page based on the WordPress Template hierarchy.
File:Template Hierarchy.png smallFull-Size Image (png) ; Source: wp-tricks.co.il/small
The Template Hierarchy In Detail
The following sections describe the order in which template files are being called by WordPress for each query type.
Home Page display
- tt style=font-weight:bold; color:#036home.php/tt
- tt style=font-weight:bold; color:#036index.php/tt
Single Post display
- tt style=font-weight:bold; color:#036single-{post_type}.php/tt - If the post_type were tt style=font-weight:bold; color:#036videos/tt, WordPress would look for tt style=font-weight:bold; color:#036single-videos.php/tt.
- tt style=font-weight:bold; color:#036single.php/tt
- tt style=font-weight:bold; color:#036index.php/tt
Page display
- Page Templates:
- tt style=font-weight:bold; color:#036custom template/tt - Where custom template is the Page Template assigned to the Page.
- tt style=font-weight:bold; color:#036page-{slug}.php/tt - If the page slug is recent-news, WordPress will look to use tt style=font-weight:bold; color:#036page-recent-news.php/tt
- tt style=font-weight:bold; color:#036page-{id}.php/tt - If the page ID is 6, WordPress will look to use tt style=font-weight:bold; color:#036page-6.php/tt
- tt style=font-weight:bold; color:#036page.php/tt
- tt style=font-weight:bold; color:#036index.php/tt
Front Page display
- tt style=font-weight:bold; color:#036front-page.php/tt - Used for both Your latest posts or A static page as set in the Front page displays section of Administration Settings Reading
- Page display rules - When Front page is set in the Front page displays section of Administration Settings Reading
- Home Page display rules - When Posts page is set in the Front page displays section of Administration Settings Reading
Category display
- Category Templates:
- tt style=font-weight:bold; color:#036category-{slug}.php/tt - If the category's slug were tt style=font-weight:bold; color:#036news/tt, WordPress would look for tt style=font-weight:bold; color:#036category-news.php/tt
- tt style=font-weight:bold; color:#036category-{id}.php/tt - If the category's ID were tt style=font-weight:bold; color:#0366/tt, WordPress would look for tt style=font-weight:bold; color:#036category-6.php/tt
- tt style=font-weight:bold; color:#036category.php/tt
- tt style=font-weight:bold; color:#036archive.php/tt
- tt style=font-weight:bold; color:#036index.php/tt
Tag display
- Tag Templates:
- tt style=font-weight:bold; color:#036tag-{slug}.php/tt - If the tag's slug were tt style=font-weight:bold; color:#036sometag/tt, WordPress would look for tt style=font-weight:bold; color:#036tag-sometag.php/tt
- tt style=font-weight:bold; color:#036tag-{id}.php/tt - If the tag's ID were tt style=font-weight:bold; color:#0366/tt, WordPress would look for tt style=font-weight:bold; color:#036tag-6.php/tt
- tt style=font-weight:bold; color:#036tag.php/tt
- tt style=font-weight:bold; color:#036archive.php/tt
- tt style=font-weight:bold; color:#036index.php/tt
Custom Taxonomies display
- tt style=font-weight:bold; color:#036taxonomy-{taxonomy}-{term}.php/tt - If the taxonomy were tt style=font-weight:bold; color:#036sometax/tt, and taxonomy's slug were tt style=font-weight:bold; color:#036someterm/tt WordPress would look for tt style=font-weight:bold; color:#036taxonomy-sometax-someterm.php/tt
- tt style=font-weight:bold; color:#036taxonomy-{taxonomy}.php/tt - If the taxonomy were tt style=font-weight:bold; color:#036sometax/tt, WordPress would look for tt style=font-weight:bold; color:#036taxonomy-sometax.php/tt
- tt style=font-weight:bold; color:#036taxonomy.php/tt
- tt style=font-weight:bold; color:#036archive.php/tt
- tt style=font-weight:bold; color:#036index.php/tt
Author display
- Author Templates:
- tt style=font-weight:bold; color:#036author-{nicename}.php/tt - If the author's nice name were tt style=font-weight:bold; color:#036rami/tt, WordPress would look for tt style=font-weight:bold; color:#036author-rami.php/tt.
- tt style=font-weight:bold; color:#036author-{id}.php/tt - If the author's ID were tt style=font-weight:bold; color:#0366/tt, WordPress would look for tt style=font-weight:bold; color:#036author-6.php/tt.
- tt style=font-weight:bold; color:#036author.php/tt
- tt style=font-weight:bold; color:#036archive.php/tt
- tt style=font-weight:bold; color:#036index.php/tt
Date display
- Archive (Date) Templates:
- tt style=font-weight:bold; color:#036date.php/tt
- tt style=font-weight:bold; color:#036archive.php/tt
- tt style=font-weight:bold; color:#036index.php/tt
Search Result display
- Search Templates:
- tt style=font-weight:bold; color:#036search.php/tt
- tt style=font-weight:bold; color:#036index.php/tt
404 (Not Found) display
- 404 Templates:
- tt style=font-weight:bold; color:#036404.php/tt
- tt style=font-weight:bold; color:#036index.php/tt
Attachment display
- Attachment Templates:
- tt style=font-weight:bold; color:#036MIME_type.php/tt - it can be any MIME type (tt style=font-weight:bold; color:#036image.php/tt, tt style=font-weight:bold; color:#036video.php/tt, tt style=font-weight:bold; color:#036audio.php/tt, tt style=font-weight:bold; color:#036application.php/tt or any other).
- tt style=font-weight:bold; color:#036attachment.php/tt
- tt style=font-weight:bold; color:#036single.php/tt
- tt style=font-weight:bold; color:#036index.php/tt
Changelog
- 3.0 :
- single-{post_type}.php added
- author-{nicename}.php and author-{id}.php added
- taxonomy.php, taxonomy-{taxonomy}.php and taxonomy-{taxonomy}-{term}.php added
- front-page.php added
- 2.9 :
- page-{slug}.php and page-{id}.php added
- category-{slug}.php added
- tag-{id}.php added
- 2.3 :
- tag.php and tag-{slug}.php added
- 2.0 :
- attachment.php added
Source File
The template hierarchy functions are located in onlyincludecodewp-includes/theme.php/code/onlyinclude
div class=template-description style=padding: 0 1.5em; border: 1px solid #eeeeee; background-color: #f9f9f9
Template Description
Link to the source code on http://core.trac.wordpress.org/browser/.
Parameters
- filename
- (option) path to codetag/code (version) or codetrunk/code. This option is only used for a new function.br /Default: codetrunk/code -- trunk is the latest bleeding edge development version of WordPress.
Usage
Link to the stable version: pre已侦测回归模板: Template:Trac/pre
Link to trunk: pre已侦测回归模板: Template:Trac/pre
/div
wordpress.org.cn.

