WordPress 模板層次結構

 

本文來自於WordPress的官方網站文檔。可是中文版的沒有翻譯完成。這裏翻譯校對一下。php

原文地址:http://codex.wordpress.org/Template_Hierarchy前端

簡介 web

WordPress模板將主題內的文件像拼圖同樣拼在一塊兒,生成你網站的頁面。有些模板(好比頁眉和頁腳)是全部頁面公用的;可是其它的模板則只在特定的狀況下使用。app

關於ide

這篇文章回答了下面這些問題:wordpress

WordPress生成特定頁面的時候調用了什麼模板文件?函數

(注:即WordPress的模板調用規則,WordPress根據查詢字符串決定須要走哪一條規則,而後執行具體的規則)post

讀者網站

自從WordPress1.5版本開始,引入主題以來,模板的可配置性愈來愈高,爲了開發WordPress主題,你有必要了解WordPress是如何爲展示不一樣頁面選擇模板文件的。若是你但願從已有的主題作定製,這篇文章將告訴你須要修改那些模板文件。this

使用條件標籤函數判斷

Wordpress 提供多種將查詢(注:這裏的查詢是指查詢字符串,即請求的URI後面的參數)匹配到模板的方式。主題開發者也可使用條件標籤來控制特定頁面的模板使用。有些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.

首先,WordPress將每個查詢字符串匹配對應的查詢類型 - 也就是說,它決定什麼類型的頁面(搜索頁,分類頁面,主頁等)被請求。

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文件(主題的主頁模板文件)將被使用。

舉例

假設博客網址是 http://example.com/blog/ ,訪客點擊了指向某分類的連接: http://example.com/blog/category/your-cat/: WordPress將按照以下方式尋找模板文件並生成頁面。

WordPress在當前主題目錄下尋找一個匹配當前文章分類ID的模板文件。

1. 若是文章分類ID是4, WordPress會查找文件: category-4.php.

2. 若是沒有category-4.php, WordPress再找通用的文章分類模板, category.php.

3. 若是這也沒有, WordPress再找通用文章歸檔(archive)模板, archive.php.

4. 若是尚未, WordPress回到主題主模板文件, index.php.

若是訪客訪問首頁: http://example.com/blog/:

1. WordPress首先看是否有 靜態首頁. 若是有,則根據 模板層次載入那個頁面.

2. 若是沒有靜態首頁,則去到 home.php ,用它來生成請求的頁面。

3. 若是 home.php 也沒有, 再回到當前主題目錄下的index.php 文件,用它來生成頁面。

Visual Overview 層次結構概覽圖

The following diagram shows which template files are called to generate a WordPress page based on the WordPress Template hierarchy.

下圖顯示了基於WordPress的模板層次結構,會調用哪一個模板文件,生成WordPress頁面。

(注:圖中的框是根據查詢字符串決定要請求的頁面,左邊的黑邊白底框,而後跟隨圖中的箭頭,灰框和深灰框,最後使用黑框)。

clip_image002

Full-Size Image (png)  <-- 點擊這裏看下原圖

A more in depth hierarchy diagram, including template-related conditional tags and body CSS classes, can be found here.

更深刻的層次結構圖,包括模板相關的條件標籤,和HTML BODY元素的CSS類,能夠在這裏找到。

模板層次細節

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

如下各節描述了,WordPress爲每種查詢類型,調用模板文件的順序。

首頁顯示

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.

用於展示博客文章索引的模板文件,不管是在網站上首頁或靜態頁的模板文件。注:對於站點首頁,前端頁模板優先於博客文章索引模板(首頁)。

1. home.php

2. index.php

Front Page display 前端頁顯示

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.

1. front-page.php - Used for both Your latest posts or A static page as set in the Front page displays section of Settings -> Reading

2. Page display rules - When Front page is set in the Front page displays section of Settings -> Reading

3. Home Page display rules - When Posts page is set in the Front page displays section of Settings -> Reading

Single Post display 單篇文章顯示

Template file used to render a single post page.

用於展現單個post頁的文件。

1. single-{post_type}.php - If the post type were product, WordPress would look for single-product.php.

2. single.php

3. index.php

Page display Page顯示

Template file used to render a static page (page post-type)

WordPress裏面Page和Post是兩個概念,能夠本身查詢一下。

1. custom template file - The Page Template assigned to the Page. See get_page_templates().

2. page-{slug}.php - If the page slug is recent-news, WordPress will look to use page-recent-news.php

3. page-{id}.php - If the page ID is 6, WordPress will look to use page-6.php

4. page.php

5. index.php

Category display 類別顯示

Template file used to render a Category Archive Index page

1. category-{slug}.php - If the category's slug were news, WordPress would look for category-news.php

2. category-{id}.php - If the category's ID were 6, WordPress would look for category-6.php

3. category.php

4. archive.php

5. index.php

Tag display 標籤顯示

Template file used to render a Tag Archive Index page

1. tag-{slug}.php - If the tag's slug were sometag, WordPress would look for tag-sometag.php

2. tag-{id}.php - If the tag's ID were 6, WordPress would look for tag-6.php

3. tag.php

4. archive.php

5. index.php

Custom Taxonomies display 定製分類學顯示

Template file used to render the Archive Index page for a Custom Taxonomy

1. taxonomy-{taxonomy}-{term}.php - If the taxonomy were sometax, and taxonomy's term were someterm WordPress would look for taxonomy-sometax-someterm.php. In the case of Post Formats, the taxonomy is 'post_format' and the terms are 'post-format-{format}. i.e. taxonomy-post_format-post-format-link.php

2. taxonomy-{taxonomy}.php - If the taxonomy were sometax, WordPress would look for taxonomy-sometax.php

3. taxonomy.php

4. archive.php

5. index.php

Custom Post Types display 定製文章類型顯示

Template file used to render the Archive Index page for a Custom Post Type

1. archive-{post_type}.php - If the post type were product, WordPress would look for archive-product.php.

2. archive.php

3. index.php

(For rendering a single custom post type, refer to the Single Post display section above.)

Author display 做者顯示

Template file used to render an Author Archive Index page

1. author-{nicename}.php - If the author's nice name were rami, WordPress would look for author-rami.php.

2. author-{id}.php - If the author's ID were 6, WordPress would look for author-6.php.

3. author.php

4. archive.php

5. index.php

Date display 日期顯示

Template file used to render a Date-Based Archive Index page

1. date.php

2. archive.php

3. index.php

Search Result display 搜索結果顯示

Template file used to render a Search Results Index page

1. search.php

2. index.php

404 (Not Found) display 404頁面顯示

Template file used to render a Server 404 error page

1. 404.php

2. index.php

Attachment display 附件顯示

Template file used to render a single attachment (attachment post-type) page

1. MIME_type.php - it can be any MIME type (image.php, video.php, application.php). For text/plain, in order:

1. text.php

2. plain.php

3. text_plain.php

2. attachment.php

3. single-attachment.php

4. single.php

5. index.php

Filter Hierarchy 過濾層次結構

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.

WordPress的模板系統容許您篩選的層次結構(注:即更改默認查找模板的順序,本身控制查找模板的順序)。過濾器(位於get_query_template()函數)使用此過濾器的名稱:「{$type}_template」,這裏$ type是類型層次結構中的文件名,不加.php擴展名。

Full list:

· index_template

· 404_template

· archive_template

· author_template

· category_template

· tag_template

· taxonomy_template

· date_template

· home_template

· front_page_template

· page_template

· paged_template

· search_template

· single_template

· text_template, plain_template, text_plain_template (all mime types)

· attachment_template

· comments_popup

舉例

For example, let's take the default author hierarchy:

· author-{nicename}.php

· author-{id}.php

· author.php

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.

要在模板調用順序鏈中,author.php以前添加author-{role}.php ,咱們可使用author_template鉤子 操縱實際的調用層次。這就能夠處理形如 /author/username的請求,(該用戶具備編輯角色),WordPress若是在當前主題目錄中找到author-editor.php ,就調用顯示它。

 1 <?php
 2 <?php
 3 function author_role_template($templates = '')
 4 {
 5     $author = get_queried_object ();
 6     $role = $author->roles [0];
 7     if (! is_array ( $templates ) && ! empty ( $templates )) {
 8         $templates = locate_template ( array (
 9                 "author-$role.php",
10                 $templates
11         ), false );
12     }
13     elseif (empty ( $templates )) {
14         $templates = locate_template ( "author-$role.php", false );
15     }
16     else {
17         $new_template = locate_template ( array (
18                 "author-$role.php"
19         ) );
20         if (! empty ( $new_template ))
21             array_unshift ( $templates, $new_template );
22     }
23     return $templates;
24 }
25 add_filter ( 'author_template', 'author_role_template' );
26 ?>
View Code
相關文章
相關標籤/搜索