WordPress的模板层次结构决定了主题如何根据请求类型选择模板文件来渲染页面。理解这一结构有助于开发者创建自定义主题或修改现有主题。
1. 首页(Home Page)
front-page.php:如果存在,用于渲染首页。- 如果不存在,WordPress会使用
home.php。 - 如果
home.php也不存在,则使用index.php。
2. 文章页(Single Post)
single-{post_type}-{slug}.php:首先查找与文章类型和别名匹配的模板,如single-book-mystery.php。single-{post_type}.php:如果不存在,查找与文章类型匹配的模板,如single-book.php。single.php:如果以上都不存在,使用single.php。- 最后,使用
index.php。
3. 页面(Page)
custom-template.php:如果页面使用了自定义模板。page-{slug}.php:查找与页面别名匹配的模板,如page-about.php。page-{id}.php:查找与页面ID匹配的模板,如page-2.php。page.php:如果以上都不存在,使用page.php。- 最后,使用
index.php。
4. 分类页(Category)
category-{slug}.php:查找与分类别名匹配的模板,如category-news.php。category-{id}.php:查找与分类ID匹配的模板,如category-6.php。category.php:如果以上都不存在,使用category.php。archive.php:如果category.php不存在,使用archive.php。- 最后,使用
index.php。
5. 标签页(Tag)
tag-{slug}.php:查找与标签别名匹配的模板,如tag-snippet.php。tag-{id}.php:查找与标签ID匹配的模板,如tag-3.php。tag.php:如果以上都不存在,使用tag.php。archive.php:如果tag.php不存在,使用archive.php。- 最后,使用
index.php。
6. 自定义分类法(Custom Taxonomy)
taxonomy-{taxonomy}-{term}.php:查找与分类法和术语匹配的模板,如taxonomy-genre-scifi.php。taxonomy-{taxonomy}.php:查找与分类法匹配的模板,如taxonomy-genre.php。taxonomy.php:如果以上都不存在,使用taxonomy.php。archive.php:如果taxonomy.php不存在,使用archive.php。- 最后,使用
index.php。
7. 作者页(Author)
author-{nicename}.php:查找与作者昵称匹配的模板,如author-john.php。author-{id}.php:查找与作者ID匹配的模板,如author-1.php。author.php:如果以上都不存在,使用author.php。archive.php:如果author.php不存在,使用archive.php。- 最后,使用
index.php。
8. 日期页(Date)
date.php:如果存在,用于渲染日期归档页。archive.php:如果date.php不存在,使用archive.php。- 最后,使用
index.php。
9. 搜索结果页(Search Results)
search.php:如果存在,用于渲染搜索结果页。- 最后,使用
index.php。
10. 404错误页(404 Error)
404.php:如果存在,用于渲染404错误页。- 最后,使用
index.php。
11. 附件页(Attachment)
MIME_type.php:查找与附件MIME类型匹配的模板,如image.php。attachment.php:如果以上都不存在,使用attachment.php。single.php:如果attachment.php不存在,使用single.php。- 最后,使用
index.php。
12. 嵌入页(Embed)
embed-{post_type}-{post_format}.php:查找与文章类型和格式匹配的模板,如embed-post-video.php。embed-{post_type}.php:查找与文章类型匹配的模板,如embed-post.php。embed.php:如果以上都不存在,使用embed.php。- 最后,使用
index.php。
13. 自定义文章类型归档页(Custom Post Type Archive)
archive-{post_type}.php:查找与自定义文章类型匹配的模板,如archive-book.php。archive.php:如果以上都不存在,使用archive.php。- 最后,使用
index.php。
14. 分页(Paged)
- 如果当前页面是分页页面,WordPress会在上述模板中查找分页内容。
WordPress的模板层次结构非常灵活,允许开发者通过创建特定模板文件来精确控制页面渲染。理解这一结构有助于高效开发和维护主题。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
















暂无评论内容