# 模板文件
## 話題
* [模板術語](https://developer.wordpress.org/themes/basics/template-files/#template-terminology)
* [模板文件](https://developer.wordpress.org/themes/basics/template-files/#template-files)
* [模板部分](https://developer.wordpress.org/themes/basics/template-files/#template-partials)
* [常見的WordPress模板文件](https://developer.wordpress.org/themes/basics/template-files/#common-wordpress-template-files)
* [使用模板文件](https://developer.wordpress.org/themes/basics/template-files/#using-template-files)
模板文件用于整個WordPress主題,但首先讓我們了解術語。
## 模板術語[#模板術語](https://developer.wordpress.org/themes/basics/template-files/#template-terminology)
在處理WordPress主題時,術語“模板”以不同的方式使用:
* 模板文件存在于主題中,并表示您的網站顯示方式。
* [頁面模板](https://developer.wordpress.org/themes/template-files-section/page-template-files/ "頁面模板")是*僅?*適用?于頁面以更改其外觀的[模板](https://developer.wordpress.org/themes/template-files-section/page-template-files/ "Page Templates")。頁面模板可以應用于單個頁面,頁面部分或一類頁面。
* [模板標簽](https://developer.wordpress.org/themes/basics/template-tags/ "模板標簽")是WordPress的內置功能,您可以在模板文件中使用它來檢索和顯示數據(例如[`the_title()`](https://developer.wordpress.org/reference/hooks/the_title/ "功能參考/標題")和[`the_content()`](https://developer.wordpress.org/reference/hooks/the_content/ "功能參考/內容"))。
* [模板層次結構](https://developer.wordpress.org/themes/basics/template-hierarchy/ "模板層次結構")是WordPress根據所請求的內容來決定使用哪個主題模板文件的邏輯。
[頂部↑](https://developer.wordpress.org/themes/basics/template-files/#top)
## 模板文件[#模板文件](https://developer.wordpress.org/themes/basics/template-files/#template-files)
WordPress主題由模板文件組成。這些是包含HTML,[模板標記](https://developer.wordpress.org/themes/basics/template-tags/ "模板標簽")和PHP代碼的混合的PHP文件。
構建主題時,將使用模板文件來影響網站不同部分的布局和設計。例如,您將使用`header.php`模板創建標題,或使用`comments.php`模板包含注釋。
當有人訪問您網站上的頁面時,WordPress會根據請求加載模板。模板文件顯示的內容類型由與模板文件關聯的[發布類型](https://developer.wordpress.org/themes/basics/post-types/)確定。該?[模板層次](https://developer.wordpress.org/themes/basics/template-hierarchy/ "模板層次結構")?描述了模板文件的WordPress將加載基于請求的類型以及是否模板主題存在。然后,服務器解析模板中的PHP,并將HTML返回給訪問者。
最關鍵的模板文件是`index.php`,如果在[模板層次結構中](https://developer.wordpress.org/themes/basics/template-hierarchy/)找不到更具體的模板,則該模板文件是全部模板。盡管主題僅需要一個?`index.php`模板,但是通常主題包含許多模板以顯示不同的內容類型和上下文。
[頂部↑](https://developer.wordpress.org/themes/basics/template-files/#top)
## 模板諧音[#模板部分](https://developer.wordpress.org/themes/basics/template-files/#template-partials)
模板部分是模板的一部分,它作為另一個模板(例如網站標題)的一部分包括在內。模板部分可以嵌入多個模板中,從而簡化主題創建。常見的模板部分包括:
* `header.php`用于生成網站的標題
* `footer.php`用于生成頁腳
* `sidebar.php`用于生成側邊欄
盡管上述模板文件在WordPress中是特殊情況,并且僅適用于頁面的一部分,但是您可以創建任意數量的模板部分并將它們包括在其他模板文件中。
[頂部↑](https://developer.wordpress.org/themes/basics/template-files/#top)
## 常見的WordPress模板文件[#常見的WordPress模板文件](https://developer.wordpress.org/themes/basics/template-files/#common-wordpress-template-files)
以下是WordPress可以識別的一些基本主題模板和文件的列表。
**index.php**
主模板文件。在所有主題中都是**必需的**。
**style.css**
主要樣式表。在所有主題中都是**必需的**,并且包含您主題的信息標題。
**rtl.css**
如果網站語言的文本方向是從右到左,則將自動包含從右到左的樣式表。
**comments.php**
評論模板。
**front-page.php**
不管**管理>設置>閱讀**上的什么設置,首頁模板都始終用作網站首頁(如果存在)。
**home.php**
主頁模板默認為首頁。如果未將WordPress設置為使用靜態首頁,則此模板用于顯示最新帖子。
**header.php**
標頭模板文件通常包含您站點的文檔類型,元信息,樣式表和腳本的鏈接以及其他數據。
**singular.php**
如果`single.php`找不到帖子,則使用單個模板;如果找不到頁面,`page.php`則用于頁面。如果`singular.php`找不到,`index.php`是
**single.php**
當訪客請求單個帖子時,將使用單個帖子模板。
**single- {post-type} .php**
訪客從自定義帖子類型請求單個帖子時使用的單個帖子模板。例如,`single-book.php`將用于顯示來自名為*book*的自定義帖子類型的單個帖子。這`index.php`
如果不存在用于自定義帖子類型的特定查詢模板,則使用此選項。
**archive- {post-type} .php**
當訪問者請求自定義帖子類型存檔時,將使用存檔帖子類型模板。例如,`archive-books.php`將用于顯示來自名為*books*的自定義帖子類型的帖子存檔。<
如果`archive-{post-type}.php`沒有,則使用archive.php模板文件。
**page.php**
當訪客請求單個頁面(內置模板)時,將使用頁面模板。
**頁面-{slug} .php**
訪問者請求特定頁面(例如帶有“ about”塊(page-about.php)的頁面)時,將使用頁面塊模板。
**category.php**
當訪客按類別請求帖子時,將使用類別模板。
**tag.php**
當訪客按標簽請求發布時,將使用標簽模板。
**taxonomy.php**
當訪客請求自定義分類法中的術語時,將使用分類法術語模板。
**author.php**
每當訪問者加載作者頁面時,都會使用作者頁面模板。
**date.php**
當按日期或時間請求帖子時,將使用日期/時間模板。例如,使用這些條生成的頁面:
http://example.com/blog/2014/
http://example.com/blog/2014/05/
http://example.com/blog/2014/05/ 26 /
**archive.php**
當訪問者按類別,作者或日期請求帖子時,將使用存檔模板。**注意**:如果存在更具體的模板(如`category.php`和)`author.php`,則此模板將被覆蓋`date.php`。
**search.php**
搜索結果模板用于顯示訪問者的搜索結果。
**attachment.php**
當查看單個附件(例如圖像,pdf或其他媒體文件)時,將使用附件模板。
**image.php**
圖像附件模板是的更特定版本,`attachment.php`在查看單個圖像附件時使用。如果不存在,則將使用WordPress`attachment.php`。
**404.php**
當WordPress無法找到符合訪問者請求的帖子,頁面或其他內容時,將使用404模板。
[頂部↑](https://developer.wordpress.org/themes/basics/template-files/#top)
## 使用模板文件[#使用模板文件](https://developer.wordpress.org/themes/basics/template-files/#using-template-files)
在WordPress模板中,您可以使用[模板標簽](https://developer.wordpress.org/themes/basics/template-tags/ "模板標簽")動態顯示信息,包含其他模板文件或自定義網站。
例如,在您中,`index.php`您可以在最終生成的頁面中包括其他文件:
* 要包含標題,請使用[get\_header()](https://developer.wordpress.org/reference/functions/get_header/ "功能參考/獲取標題")
* 要包含側邊欄,請使用[get\_sidebar()](https://developer.wordpress.org/reference/functions/get_sidebar/ "功能參考/獲取側欄")
* 要包含頁腳,請使用[get\_footer()](https://developer.wordpress.org/reference/functions/get_footer/ "功能參考/獲取頁腳")
* 要包含搜索表單,請使用[get\_search\_form()](https://developer.wordpress.org/reference/functions/get_search_form/ "功能參考/獲取搜索表")
* 要包含自定義主題文件,請使用[get\_template\_part()](https://developer.wordpress.org/reference/functions/get_template_part/ "功能參考/獲取模板部分")
這是一個WordPress模板標簽示例,可?*將*特定模板*包含*到您的頁面中:
1個2個3<?php get_sidebar(); ?><?php get_template_part( 'featured-content' ); ?><?php get_footer(); ?>
您可以在“[模板標簽”](https://developer.wordpress.org/themes/basics/template-tags/ "模板標簽")上找到整頁內容,以了解有關它們的全部信息。
有關鏈接組件模板的更多信息,請參考“[鏈接主題文件和目錄](https://developer.wordpress.org/themes/basics/linking-theme-files-directories/ "鏈接主題文件和目錄")”部分。