导读:主题文件构在开始制作 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