WordPress:Template Hierarchy

来自站长百科
Seadragon530讨论 | 贡献2008年4月17日 (四) 13:40的版本
跳转至: 导航、​ 搜索
注意: 本文关于一个高级话题。参见走进模板作为接触WordPress模板的介绍.


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.


介绍

WordPress模板结合在一起,就象一个难题的各个部分一样,生成了你的WordPress站点上的网页。一些模板(例如页眉和页脚模板文件)可以用在所有的网页上,而另外一写只能在特殊情况下使用。


What this article is about

This article seeks to answer the following question:

Which template file(s) will WordPress use when it displays a certain type of page?


本文是关于什么的

本文致力于寻找下面问题的答案:

当WordPress显示特定的页面类型时,会使用哪种模板文件呢?


Who might find this useful

Since the introduction of Themes in WordPress v1.5, WordPress: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.

本文适用对象

由于WordPress v1.5 中对主题的介绍,模板已经越来越可以自由配置使用了。为了开发 WP主题,要正确的理解WordPress选择模板文件在blog上显示多种页面的方式。如果你想要自定义一个已存在的WordPress主题,本文将帮助你确定哪个模板文件需要编辑。

Conditional Tags and the Template Hierarchy

WordPress 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 Theme Development and WordPress:Conditional Tags pages for more.


条件式标签和模板层次

WordPress提供了很多方法来配合模板查询类型。WordPress主题开发人员还使用:Theme_Development#Query-based_Templates|条件式标签]]来控制生成一个特定的页面时使用哪个模板。一些WordPress主题可能不能贯彻所有这里说到的模板文件。一些主题Some 使用条件式标签来载入其他的模板文件。参见主题开发条件式标签


The Template File Hierarchy

The General Idea

WordPress uses the Query String — information contained within each link on your web site — to decide which template or set of templates will be used to display the page.

模板文件层次

概述

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.

首先,WordPress把每个字符传查询与类型查询匹配— i.e.决定哪种类型的页面(搜索页面,分类页面,主页等等.)是所需要的。

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主题中哪些模板可用,按照WordPress模板层次的建议排序。

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.

WordPress在现用的主题目录中通过特定的名字寻找模板文件,使用下边列在合适的查询结果中的第一匹配的模板文件。

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.


除了基本的index.php模板文件以外,主题开发人员可以选择是否使用某个特定的模板文件。如果WordPress不能使用匹配名字找到相应的模板,它会跳到这个层次中的下一个文件名。如果WordPress不能找到所有的匹配模板文件,index.php(主题主页模板文件)。

Examples

If your blog is at http://domain.com/wp/ and a visitor clicks on a link to a category page like http://domain.com/wp/category/your-cat/, 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 category-4.php. If it is missing, WordPress next looks for a generic category template file, category.php. If this file does not exist either, WordPress looks for a generic archive template, archive.php. If it is missing as well, WordPress falls back on the main Theme template file, index.php.

示例

如果你的blog在http://domain.com/wp/,并且访问者点击了一个分类页面连接如http://domain.com/wp/category/your-cat/,WordPress会在现有的主题目录中寻找匹配的分类ID。如果这个分类ID是4,WordPress会去寻找一个名字为category-4.php的模板文件。如果没有找到,WordPress就会去寻找普通分类模板文件,category.php。如果这个文件也不存在,WordPress就会寻找普通文档模板,archive.php。如果还是没有找到,WordPress就会返回到主页模板文件,index.php.

If a visitor goes to your home page at http://domain.com/wp/, WordPress looks for a template file called home.php and uses it to generate the requested page. If home.php is missing, WordPress looks for a file called index.php in the active theme's directory, and uses that template to generate the page.

如果访问者到了你位于http://domain.com/wp/的主页,WordPress就会寻找一个叫做home.php的模板文件,并且使用它生成请求的页面。如果没有找到home.php,WordPress就会在主题目录中寻找一个叫做index.php的文件,使用这个模板来生成页面。


Visual Overview

The following diagram shows which template files are called to generate a WordPress page based on the WordPress Template hierarchy. It also illustrates the relationship of Query Strings to corresponding WordPress:Conditional Tags.

视觉总览

以下代码显示出哪个模板文件被用来生成一个基于WordPress模板层次的WordPress页面了。它还描述了字符串查询和相应的条件式标签的关系

|Graphic Example of the WordPress Template Hierarchy Structure

Full-Size Image (png)

Download ArgoUML-0.22-File (zargo) (external link)

| WordPress模板层次结构图例

Full-Size Image (png)

Download ArgoUML-0.22-File (zargo) (external link)

The Template Hierarchy In Detail

The following sections describe the order in which template files are being called by WordPress for each query type.

模板层次详解

以下部分描述了在模板文件中WordPress调用的每个查询类型的顺序。


The Main page

  1. home.php
  2. index.php

Single Post page

  1. single.php
  2. index.php

WordPress Page

  1. The template selected from the "Page Template" dropdown when editing the Page.
  2. page.php
  3. index.php
Note: This section refers to WordPress Pages, not generic posts or web pages from your blog. Please see WordPress Pages for details.

主页

  1. home.php
  2. index.php

单个文章页面

  1. single.php
  2. index.php

WordPress 页面

  1. 编辑页面的时候,从"页面模板"下拉菜单中选择模板.
  2. page.php
  3. index.php
注意: 这里指的是WordPress 页面, 不是blog中的一般文章或者网页。参见 WordPress 页面.


Category page

  1. The Category Template with a matching ID. If the category's ID were 6, WordPress would look for category-6.php
  2. category.php
  3. archive.php
  4. index.php
Note: See WordPress:Category Templates for details.

分类页面

  1. 分类模板有一个匹配ID. 如果分类的ID是6, WordPress 就会寻找category-6.php
  2. category.php
  3. archive.php
  4. index.php
Note: See WordPress:Category Templates for details.


Tag page

  1. The Tag Template with a matching slug. If the tag's slug were sometag, WordPress would look for tag-sometag.php
  2. tag.php
  3. archive.php
  4. index.php
Note: See WordPress:Tag Templates for details.

标签页面

  1. 标签模板有一个匹配插槽.如果标签插槽是sometag, WordPress就会搜索 tag-sometag.php
  2. tag.php
  3. archive.php
  4. index.php
Note: See WordPress:Tag Templates for details.


Author page

  1. author.php
  2. archive.php
  3. index.php
Note: The Author Template can be used to display information about authors.

作者页面

  1. author.php
  2. archive.php
  3. index.php
注意:作者模板 可以用来显示有关作者的信息.


Date page

For example, a monthly archive page.

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

日期页面

举个例子,一个按月的文档页面。

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


Search Result page

  1. search.php
  2. index.php

搜索结果页面

  1. search.php
  2. index.php


404 (Not Found) page

  1. 404.php
  2. index.php
Note: See WordPress:Creating an Error 404 Page for more information about the 404 template.

404 (找不到) 页面

  1. 404.php
  2. index.php
注意: 参见建立一个错误404页面以获得更多有关404页面的信息。


Attachment page

  1. attachment.php
  2. index.php
Note: See Image attachments for more information about this template.

附件页面

  1. attachment.php
  2. index.php
注意: 参见图片附件获得更多有关此模板的信息.


See also

The following links have direct relevance to the topic of this article.

WordPress:Theme Development | WordPress:The Loop | WordPress:Conditional Tags | WordPress:Template Tags | WordPress:Using Permalinks

参见

以下连接与文章话题直接相关.

主题开发 | 循环 | 条件式标签 | 模板标签 | 使用Permalinks