Languages: English • Français • Italiano • 日本語 한국어 • Русский • Español • Português do Brasil • 中文(简体) • (Add your language)
WordPress模板像拼图一样拼在一起,来生成你网站的页面。有些模板,比如页眉和页脚,是所有页面公用的;也有的只在一些特定页面/情况中使用。
这篇文章回答了下面这些问题:
WordPress生成特定页面的时候调用了什么模板文件?
自从WordPress引入主题以来,模板的可配置性越来越高,为了开发WordPress主题,你有必要了解WordPress是如何为不同页面选择模板文件的。如果你希望自定义一个现有的主题,这篇文章将告诉你需要修改那些模板文件。
Wordpress 提供多种将查询匹配到模板的方式。主题开发者也可以使用条件标签来控制特定页面的模板使用。有些Wordpress主题可能并未实现这里提到的所有功能。有些主题则使用条件标签来载入其他模板文件。参见条件标签页面和"基于查询"的主题开发。
Wordpress使用查询字符串——你网站中每个链接所包含的信息,来决定使用哪个或哪些模板文件。
First, WordPress matches every Query String to query types — 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 — and web page content is generated — 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 index.php 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, index.php (the Theme's home page template file) will be used.
如果你的博客网址是 http://example.com/blog/ ,有一个访客点击了其中一个分类的链接: http://example.com/blog/category/your-cat/: WordPress将按照如下方式寻找模板文件并生成页面。
WordPress在当前主题目录下寻找一个匹配当前文章分类ID的模板文件。
如果访客访问了你的首页: http://example.com/blog/:
The following diagram shows which template files are called to generate a WordPress page based on the WordPress Template hierarchy.
A more in depth hierarchy diagram, including template-related conditional tags and body CSS classes, can be found here.
The following sections describe the order in which template files are being called by WordPress for each query type.
Template file used to render the Blog Posts Index, whether on the site front page or on a static page. Note: on the Site Front Page, the Front Page template takes precedence over the Blog Posts Index (Home) template.
Template file used to render the Site Front Page, whether the front page displays the Blog Posts Index or a static page. The Front Page template takes precedence over the Blog Posts Index (Home) template.
Template file used to render a single post page.
Template file used to render a static page (page post-type)
Template file used to render a Category Archive Index page
Template file used to render a Tag Archive Index page
Template file used to render the Archive Index page for a Custom Taxonomy
Template file used to render the Archive Index page for a Custom Post Type
(For rendering a single custom post type, refer to the Single Post display section above.)
Template file used to render an Author Archive Index page
Template file used to render a Date-Based Archive Index page
Template file used to render a Search Results Index page
Template file used to render a Server 404 error page
Template file used to render a single attachment (attachment post-type) page
The WordPress templates system allow you to filter the hierarchy. The filter (located in the get_query_template() function) uses this filter name: "{$type}_template" where $type is the a file name in the hierarchy without the .php extension.
Full list:
For example, let's take the default author hierarchy:
To add author-{role}.php before author.php we can manipulate the actual hierarchy using the 'author_template' hook. This allows a request for /author/username where username has the role of editor to display using author-editor.php if present in the current themes 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' );
wp-includes/theme.php
.wp-includes/template-loader.php
.Template Hierarchy: Category Templates, Tag Templates, Taxonomy Templates, Page Templates, Post Type Templates, Author Templates, Date Templates, Search Templates, 404 Templates, Attachment Templates, Loop Templates