导读:主题文件构在开始制作 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 识别,等于废物。
主页:
- home.php
- index.php
文章页:
- single-{post_type}.php – 如果文章类型是 videos(即视频),WordPress 就会去查找 single-videos.php(WordPress 3.0 及以上版本支持)
- single.php
- index.php
页面:
- 自定义模板 – 在 WordPress 后台创建页面的地方,右侧边栏可以选择页面的自定义模板
- page-{slug}.php – 如果页面的缩略名是 news,WordPress 将会查找 page-news.php(WordPress 2.9 及以上版本支持)
- page-{id}.php – 如果页面 ID 是 6,WordPress 将会查找 page-6.php
- page.php
- index.php
分类:
- category-{slug}.php – 如果分类的缩略名为 news,WordPress 将会查找 category-news.php(WordPress 2.9 及以上版本支持)
- category-{id}.php -如果分类 ID 为 6,WordPress 将会查找 category-6.php
- category.php
- archive.php
- index.php
标签
- tag-{slug}.php – 如果标签缩略名为 sometag,WordPress 将会查找 tag-sometag.php
- tag-{id}.php – 如果标签 ID 为 6,WordPress 将会查找 tag-6.php(WordPress 2.9 及以上版本支持)
- tag.php
- archive.php
- index.php
作者:
- author-{nicename}.php – 如果作者的昵称为 rami,WordPress 将会查找 author-rami.php(WordPress 3.0 及以上版本支持)
- author-{id}.php – 如果作者 ID 为 6,WordPress 将会查找 author-6.php(WordPress 3.0 及以上版本支持)
- author.php
- archive.php
- index.php
日期页面:
- date.php
- archive.php
- index.php
搜索结果:
- search.php
- index.php
404 (未找到) 页面
- 404.php
- index.php
附件页面:
- MIME_type.php – 可以是任何 MIME 类型 (image.php, video.php, audio.php, application.php 或者其他).
- attachment.php
- single.php
- index.php
详细说明,请看 WordPress 官方文档 (如果你能看懂英文):
http://codex.WordPress.org/Template_Hierarchy