WordPress模板像拼图一样拼在一起,来生成你网站的页面。有些模板,比如页眉和页脚,是所有页面公用的;也有的只在一些特定页面/情况中使用。
WordPress生成特定页面的时候调用了什么模板文件?
如果你的博客网址是 http://example.com/blog/ ,有一个访客点击了其中一个分类的链接: http://example.com/blog/category/your-cat/: WordPress将按照如下方式寻找模板文件并生成页面。
WordPress在当前主题目录下寻找一个匹配当前文章分类ID的模板文件。
如果文章分类ID是4, WordPress会找这样一个文件: category-4.php.
如果没有这个文件, WordPress再找通用的文章分类模板, category.php.
如果这也没有, WordPress再找通用文章归档模板, archive.php.
如果还没有, WordPress回到主题主模板文件, index.php.
如果访客访问了你的首页: http://example.com/blog/:
WordPress首先看是否有 静态首页. 如果有,则根据 模板层次载入那个页面.
如果没有静态首页,则去到 home.php ,用它来生成请求的页面。
如果 home.php 也没有, 再回到当前主题目录下的index.php 文件,用它来生成页面。
渲染单页
single-{post_type}.php – 如果发布类型为product, WordPress将解析模板 single-product.php.
single.php
index.php
custom template file – 页面设置决定。
注意:自定义模板需要模板格式:
<?php /* Template Name: 实验室模板 */ ?>
page-{slug}.php – 如果页面设置模板为 recent-news, 将会调用 page-recent-news.php
page-{id}.php – 如果页面id=6, 将调用 page-6.php
page.php
index.php
category-{slug}.php – 如果分类的 slug = news,将调用模板 category-news.php
category-{id}.php – 如果分类的ID = 6,将调用模板 category-6.php
category.php
archive.php
index.php
tag-{slug}.php – If the tag’s slug were sometag, WordPress would look for tag-sometag.php
tag-{id}.php – If the tag’s ID were 6, WordPress would look for tag-6.php
tag.php
archive.php
index.php
author-{nicename}.php – If the author’s nice name were rami, WordPress would look for author-rami.php.
author-{id}.php – If the author’s ID were 6, WordPress would look for author-6.php.
author.php
archive.php
index.php
search.php
index.php
404.php
index.php
来源: 雨林博客(pancha.oss-cn-chengdu.aliyuncs.com)