主頁顯示
默認情況下,WordPress會設置您網站的主頁以顯示您的最新博客文章。此頁面稱爲博客文章索引。您還可以將博客文章設置爲顯示在單獨的靜態頁面上。home.php
無論是用作首頁還是單獨的靜態頁面,模板文件均用於呈現博客文章索引。如果 home.php
不存在,WordPress將使用 index.php
。
home.php
index.php
❗️如果front-page.php
存在,它將覆蓋 home.php 模板。
首頁顯示
front-page.php無論首頁顯示博客文章索引(如上所述)還是靜態頁面,模板文件均用於呈現網站的首頁。主頁模板優先於博客文章索引(home.php)模板。如果front-page.php文件不存在,WordPress將根據設置→閱讀中的設置使用home.php或page.php文件。如果這些文件都不存在,它將使用該index.php文件。
front-page.php
–用於“ 設置”→“閱讀” 首頁顯示部分中設置的“ 您的最新帖子 ”或“ 靜態頁面 ” 。home.php
–如果WordPress無法找到,front-page.php
並且在首頁顯示部分中設置了“ 您的最新帖子 ” ,它將查找。此外,當在首頁顯示部分中設置帖子頁面時,WordPress會查找此文件。home.php
page.php
–在首頁顯示區域中設置“ 首頁 ”時。index.php
–在首頁顯示區域中設置“ 您的最新帖子 ” 但 不存在時,或 在首頁中設置但不存在時。home.phppage.php
如您所見,WordPress採取的路徑有很多規則。使用上面的圖表是確定WordPress將顯示什麼的最佳方法。
隱私權政策頁面顯示
該privacy-policy.php
模板文件是用來提供您網站的隱私政策頁。隱私策略頁面模板優先於靜態頁面(page.php
)模板。如果privacy-policy.php
文件不存在,WordPress將根據可用模板使用page.php
或singular.php
文件。如果這些文件都不存在,它將使用該index.php文件。
privacy-policy.php
–用於設置→隱私的更改您的隱私政策頁面部分中設置的隱私政策頁面。custom template file
– 分配給頁面的頁面模板。請參閱get_page_templates
()。page-{slug}.php
–如果頁面上沒有標籤privacy,WordPress將使用page-privacy.php
。page-{id}.php
–如果頁面ID爲6,WordPress將使用page-6.php。page.php
singular.php
index.php
單帖
單個帖子模板文件用於呈現單個帖子。WordPress使用以下路徑:
single-{post-type}-{slug}.php
–(自4.4開始)首先,WordPress爲特定帖子尋找模板。例如,如果帖子類型爲product,而帖子標籤爲dmc-12
,則WordPress將查找single-product-dmc-12.php
。
2.single-{post-type}.php
–如果帖子類型爲product
,WordPress將尋找single-product.php
。
3.single.php
–然後WordPress退回到single.php
。
4.singular.php
–然後回落到singular.php
。index.php
–最後,如上所述,WordPress最終回退到index.php
。
單頁
用於呈現靜態頁面的模板文件(pagepost-type)。請注意,與其他帖子類型不同,pageWordPress是專用的,並使用以下路徑:
custom template file – 分配給頁面的頁面模板。請參閱get_page_templates()。
page-{slug}.php
–如果頁面上沒有標籤recent-news,WordPress將使用page-recent-news.php。page-{id}.php
–如果頁面ID爲6,WordPress將使用page-6.php
。page.php
singular.php
index.php
分類
呈現類別存檔索引頁面在WordPress中使用以下路徑:
category-{slug}.php
–如果類別的子類別爲news,WordPress將尋找category-news.php
。category-{id}.php
–如果類別的ID爲6,WordPress將尋找category-6.php
。category.php
archive.php
index.php
標籤
爲了顯示標籤存檔索引頁面,WordPress使用以下路徑:
tag-{slug}.php
–如果標籤的子標籤爲sometag,WordPress將尋找tag-sometag.php
。tag-{id}.php
–如果標籤的ID爲6,WordPress將尋找tag-6.php
。tag.php
archive.php
index.php
自定義分類法
自定義分類法使用的模板文件路徑略有不同:
1.taxonomy-{taxonomy}-{term}.php
-如果分類學sometax
,和分類的任期someterm,WordPress會尋找taxonomy-sometax-someterm.php
.在的情況下,後期格式,分類標準是**“post_format**”和術語是“後格式- {}格式。即taxonomy-post_format-post-format-link.php
用於鏈接發佈格式。
taxonomy-{taxonomy}.php
–如果是分類法sometax,WordPress將尋找taxonomy-sometax.php
。taxonomy.php
archive.php
index.php
自定義帖子類型
自定義帖子類型使用以下路徑來呈現適當的存檔索引頁面。
1.archive-{post_type}.php
–如果帖子類型爲product,WordPress將尋找archive-product.php
。
2. archive.php
3. index.php
作者顯示
根據以上示例,呈現作者檔案索引頁面是相當說明性的:
author-{nicename}.php
–如果作者的好名字是matt,WordPress將尋找author-matt.php
。author-{id}.php
–如果作者的ID爲6,WordPress將尋找author-6.php
。author.php
archive.php
index.php
日期
基於日期的存檔索引頁面將按您期望的方式呈現:
date.php
archive.php
index.php
搜索結果
搜索結果遵循與其他模板類型相同的模式:
search.php
index.php
404(未找到)
同樣,按以下順序調用404模板文件:
404.php
2.index.php
附件
呈現附件頁面(attachment帖子類型)使用以下路徑:
- {MIME-type}.php-可以是任何的MIME類型(例如: image.php,video.php,pdf.php)。對於text/plain,使用以下路徑(按順序):
- text-plain.php
- plain.php
- text.php
- attachment.php
- single-attachment-{slug}.php –例如,如果附件爲holiday,WordPress將尋找single-attachment-holiday.php。
- single-attachment.php
- single.php
- singular.php
- index.php
嵌入
嵌入模板文件用於呈現正在嵌入的帖子。從4.5開始,WordPress使用以下路徑:
- embed-{post-type}-{post_format}.php –首先,WordPress尋找特定帖子的模板。例如,如果其發佈類型爲post且具有音頻格式,則WordPress將尋找embed-post-audio.php。
- embed-{post-type}.php–如果帖子類型爲product,WordPress將尋找embed-product.php。
- embed.php–然後WordPress退回嵌入.php。
- 最後,WordPress最終會退回到其自己的wp-includes/theme-compat/embed.php模板。
非ASCII字符處理
下面是一個名爲的“Hello World😄”用的ID頁面的頁面模板層次6:
page-hello-world-😄.php
page-hello-world-%f0%9f%98%80.php
page-6.php
page.php
singular.php
相同的行爲適用於帖子,術語名稱和作者好記號。
過濾層次結構
WordPress模板系統可讓您過濾層次結構。這意味着您可以在層次結構的特定點插入和更改內容。過濾器(位於get_query_template()函數中)使用以下過濾器名稱:模板類型"{KaTeX parse error: Expected 'EOF', got '}' at position 5: type}̲_template"在哪裏type。
以下是模板層次結構中所有可用過濾器的列表:
- embed_template
- 404_template
- search_template
- frontpage_template
- home_template
- privacypolicy_template
- taxonomy_template
- attachment_template
- single_template
- page_template
- singular_template
- category_template
- tag_template
- author_template
- date_template
- archive_template
- index_template
Example
例如,讓我們採用默認的作者層次結構:
author-{nicename}.php
author-{id}.php
author.php
要添加author-{role}.php之前author.php,我們可以使用’author_template’模板類型來操縱實際的層次結構。這允許對/ author / username的請求,其中,如果用戶名在當前主題目錄中存在,則用戶名具有編輯者的角色,可以使用author-editor.php顯示。
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' );