導讀:主題檔案構在開始製作 WordPress 主題之前,首先得了解 WordPress 主題到底由哪些檔案構成,你得清楚 WordPress 程式是怎樣與主題檔案連線的。以

 在開始製作 WordPress 主題之前,首先得了解 WordPress 主題到底由哪些檔案構成,你得清楚 WordPress 程式是怎樣與主題檔案連線的。以下是 WordPress 預設主題 default 資料夾下的所有模板檔案:


看了上圖,可能你還摸不著頭腦,到底這些檔案是幹什麼的。 WordPress 的主題是用 PHP 編寫的,而不是純 HTML + CSS,所以模板檔案的字尾名是.php,如果你想精通 WordPress 的主題製作,完美控制你的部落格,最好要熟悉 PHP 程式設計。要是不會 PHP 程式設計怎麼辦?就做不了 WordPress 主題了嗎?那也不是,至少看完本系列教程,你也能夠掌握基本的 WordPress 主題製作方法。下面是 WordPress 主題檔案層次結構,它會告訴你:當 WordPress 顯示特定的頁面型別時,會使用哪個模板檔案呢?只有瞭解了以下主題層次結構,你才能知道你的 WordPress 主題到底需要寫哪些檔案。

怎麼看下面的檔案層次結構?
以主頁為例,下面有 2 個檔案 home.php 和 index.php,WordPress 程式會從你的主題資料夾中依次查詢這兩個檔案,如果找到 home.php,則使用 home.php 作為部落格首頁模板,即使你的主題資料夾中有 index.php;如果 home.php 未找到,則使用 index.php 作為首頁模板;如果 home.php 和 index.php 都找不到,你的主題將不會被 WordPress 識別,等於廢物。

主頁:

  1. home.php
  2. index.php

文章頁:

  1. single-{post_type}.php – 如果文章型別是 videos(即影片),WordPress 就會去查詢 single-videos.php(WordPress 3.0 及以上版本支援)
  2. single.php
  3. index.php

頁面:

  1. 自定義模板 – 在 WordPress 後臺建立頁面的地方,右側邊欄可以選擇頁面的自定義模板
  2. page-{slug}.php – 如果頁面的縮略名是 news,WordPress 將會查詢 page-news.php(WordPress 2.9 及以上版本支援)
  3. page-{id}.php – 如果頁面 ID 是 6,WordPress 將會查詢 page-6.php
  4. page.php
  5. index.php

分類:

  1. category-{slug}.php – 如果分類的縮略名為 news,WordPress 將會查詢 category-news.php(WordPress 2.9 及以上版本支援)
  2. category-{id}.php -如果分類 ID 為 6,WordPress 將會查詢 category-6.php
  3. category.php
  4. archive.php
  5. index.php

標籤

  1. tag-{slug}.php – 如果標籤縮略名為 sometag,WordPress 將會查詢 tag-sometag.php
  2. tag-{id}.php – 如果標籤 ID 為 6,WordPress 將會查詢 tag-6.php(WordPress 2.9 及以上版本支援)
  3. tag.php
  4. archive.php
  5. index.php

作者:

  1. author-{nicename}.php – 如果作者的暱稱為 rami,WordPress 將會查詢 author-rami.php(WordPress 3.0 及以上版本支援)
  2. author-{id}.php – 如果作者 ID 為 6,WordPress 將會查詢 author-6.php(WordPress 3.0 及以上版本支援)
  3. author.php
  4. archive.php
  5. index.php

日期頁面:

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

搜尋結果:

  1. search.php
  2. index.php

404 (未找到) 頁面

  1. 404.php
  2. index.php

附件頁面:

  1. MIME_type.php – 可以是任何 MIME 型別 (image.php, video.php, audio.php, application.php 或者其他).
  2. attachment.php
  3. single.php
  4. index.php

詳細說明,請看 WordPress 官方檔案 (如果你能看懂英文):

http://codex.WordPress.org/Template_Hierarchy