WordPress:Template Hierarchy

来自站长百科
Xxf3325讨论 | 贡献2008年4月10日 (四) 09:42的版本 (新页面: <div style="border: 1px solid #036; background:#eee; text-align:center; margin:5px; padding:10px">'''Note:''' This article covers an advanced topic. See [[WordPress:Stepping Into Templat...)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳转至: 导航、​ 搜索
Note: This article covers an advanced topic. See WordPress:Stepping Into Templates for an introduction to WordPress templates.


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:

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


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.

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.


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.

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.


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.

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.


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.

|Graphic Example of the WordPress Template Hierarchy Structure

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.


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.


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.


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.

Author page

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


Date page

For example, a monthly archive page.

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


Search Result page

  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.


Attachment page

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

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