您现在的位置是:首页 > cms教程 > WordPress教程WordPress教程
WordPress模板层次结构分析
冰蓝2025-03-17WordPress教程已有人查阅
导读WordPress模板将主题内的文件像拼图一样拼在一起,生成你网站的页面。有些模板(比如页眉和页脚)是所有页面公用的;但是其它的模板则只在特定的情况下使用。
简介
WordPress模板将主题内的文件像拼图一样拼在一起,生成你网站的页面。有些模板(比如页眉和页脚)是所有页面公用的;但是其它的模板则只在特定的情况下使用。
关于
这篇文章回答了下面这些问题:
WordPress生成特定页面的时候调用了什么模板文件?
(注:即WordPress的模板调用规则,WordPress根据查询字符串决定需要走哪一条规则,然后执行具体的规则)
读者
自从WordPress1.5版本开始,引入主题以来,模板的可配置性越来越高,为了开发WordPress主题,你有必要了解WordPress是如何为展现不同页面选择模板文件的。如果你希望从已有的主题做定制,这篇文章将告诉你需要修改那些模板文件。
使用条件标签函数判断
Wordpress 提供多种将查询(注:这里的查询是指查询字符串,即请求的URI后面的参数)匹配到模板的方式。主题开发者也可以使用条件标签来控制特定页面的模板使用。有些Wordpress主题可能并未实现这里提到的所有功能。有些主题则使用条件标签来载入其他模板文件。参见条件标签页面和"基于查询"的主题开发。
模版文件的分层结构
基本概念
Wordpress使用查询字符串--你网站中每个链接所包含的信息,来决定使用哪个或哪些模板文件。
First, WordPress matches every Query String to query types - i.e. it decides what type of page (a search page, a category page, the home page etc.) is being requested.
首先,WordPress将每一个查询字符串匹配对应的查询类型 - 也就是说,它决定什么类型的页面(搜索页,分类页面,主页等)被请求。
Templates are then chosen - and web page content is generated - in the order suggested by the WordPress Template hierarchy, depending upon what templates are available in a particular WordPress Theme.
这样就选定了使用的模板 - 然后生成网页内容 - 由WordPress的模板层次(即本文,具体如后文所述)的顺序,取决于特定的WordPress主题中什么模板可用(下面会看到,对于某个请求会查找一系列的模板文件,直到找到可用的)。
WordPress looks for template files with specific names in the current Theme's directory and uses the first matching template file listed under the appropriate query section below.
WordPress在当前主题的目录使用特定的名称(注:这会遵循一个从特殊到一般的过程)查找模板文件,并使用第一个匹配的模板文件,这个查找查询相关的模板文件的过程会在后面列出。
举例
假设博客网址是 http://example.com/blog/ ,访客点击了指向某分类的链接: http://example.com/blog/category/your-cat/: WordPress将按照如下方式寻找模板文件并生成页面。
WordPress在当前主题目录下寻找一个匹配当前文章分类ID的模板文件。
1. 如果文章分类ID是4, WordPress会查找文件: category-4.php.
2. 如果没有category-4.php, WordPress再找通用的文章分类模板, category.php.
3. 如果这也没有, WordPress再找通用文章归档(archive)模板, archive.php.
4. 如果还没有, WordPress回到主题主模板文件, index.php.
如果访客访问首页: http://example.com/blog/:
1. WordPress首先看是否有 静态首页. 如果有,则根据 模板层次载入那个页面.
2. 如果没有静态首页,则去到 home.php ,用它来生成请求的页面。
3. 如果 home.php 也没有, 再回到当前主题目录下的index.php 文件,用它来生成页面。
Visual Overview 层次结构概览图
The following diagram shows which template files are called to generate a WordPress page based on the WordPress Template hierarchy.
下图显示了基于WordPress的模板层次结构,会调用哪个模板文件,生成WordPress页面。
(注:图中的框是根据查询字符串决定要请求的页面,左边的黑边白底框,然后跟随图中的箭头,灰框和深灰框,之后使用黑框)。
clip_image002
Full-Size Image (png)<-- 点击这里看下原图
A more in depth hierarchy diagram, including template-related conditional tags and body CSS classes, can be found here.
更深入的层次结构图,包括模板相关的条件标签,和HTML BODY元素的CSS类,可以在这?业健?模板层次细节
The following sections describe the order in which template files are being called by WordPress for each query type.
以下各节描述了,WordPress为每种查询类型,调用模板文件的顺序。
首页显示
1. home.php
2. index.php
Front Page display 前端页显示
Template file used to render a single post page.
用于展示单个post页的文件。
Author display 作者显示
Template file used to render a Server 404 error page
1. 404.php
2. index.php
Attachment display 附件显示
WordPress模板将主题内的文件像拼图一样拼在一起,生成你网站的页面。有些模板(比如页眉和页脚)是所有页面公用的;但是其它的模板则只在特定的情况下使用。
关于
这篇文章回答了下面这些问题:
WordPress生成特定页面的时候调用了什么模板文件?
(注:即WordPress的模板调用规则,WordPress根据查询字符串决定需要走哪一条规则,然后执行具体的规则)
读者
自从WordPress1.5版本开始,引入主题以来,模板的可配置性越来越高,为了开发WordPress主题,你有必要了解WordPress是如何为展现不同页面选择模板文件的。如果你希望从已有的主题做定制,这篇文章将告诉你需要修改那些模板文件。
使用条件标签函数判断
Wordpress 提供多种将查询(注:这里的查询是指查询字符串,即请求的URI后面的参数)匹配到模板的方式。主题开发者也可以使用条件标签来控制特定页面的模板使用。有些Wordpress主题可能并未实现这里提到的所有功能。有些主题则使用条件标签来载入其他模板文件。参见条件标签页面和"基于查询"的主题开发。
模版文件的分层结构
基本概念
Wordpress使用查询字符串--你网站中每个链接所包含的信息,来决定使用哪个或哪些模板文件。
First, WordPress matches every Query String to query types - i.e. it decides what type of page (a search page, a category page, the home page etc.) is being requested.
首先,WordPress将每一个查询字符串匹配对应的查询类型 - 也就是说,它决定什么类型的页面(搜索页,分类页面,主页等)被请求。
Templates are then chosen - and web page content is generated - in the order suggested by the WordPress Template hierarchy, depending upon what templates are available in a particular WordPress Theme.
这样就选定了使用的模板 - 然后生成网页内容 - 由WordPress的模板层次(即本文,具体如后文所述)的顺序,取决于特定的WordPress主题中什么模板可用(下面会看到,对于某个请求会查找一系列的模板文件,直到找到可用的)。
WordPress looks for template files with specific names in the current Theme's directory and uses the first matching template file listed under the appropriate query section below.
WordPress在当前主题的目录使用特定的名称(注:这会遵循一个从特殊到一般的过程)查找模板文件,并使用第一个匹配的模板文件,这个查找查询相关的模板文件的过程会在后面列出。
With the exception of the basic index.php template file, Theme developers can choose whether they want to implement a particular template file or not. If WordPress cannot find a template file with a matching name, it skips down to the next file name in the hierarchy. If WordPress cannot find any matching template file, index.php (the Theme's home page template file) will be used.
除了基本的index.php模板文件,主题开发人员可以选择他们是否要提供特定模板文件的实现。如果WordPress找不到匹配名称规则的模板文件,它会继续查找层次结构中的下一个文件名。如果WordPress可以找不到任何匹配的模板文件,index.php文件(主题的主页模板文件)将被使用。举例
假设博客网址是 http://example.com/blog/ ,访客点击了指向某分类的链接: http://example.com/blog/category/your-cat/: WordPress将按照如下方式寻找模板文件并生成页面。
WordPress在当前主题目录下寻找一个匹配当前文章分类ID的模板文件。
1. 如果文章分类ID是4, WordPress会查找文件: category-4.php.
2. 如果没有category-4.php, WordPress再找通用的文章分类模板, category.php.
3. 如果这也没有, WordPress再找通用文章归档(archive)模板, archive.php.
4. 如果还没有, WordPress回到主题主模板文件, index.php.
如果访客访问首页: http://example.com/blog/:
1. WordPress首先看是否有 静态首页. 如果有,则根据 模板层次载入那个页面.
2. 如果没有静态首页,则去到 home.php ,用它来生成请求的页面。
3. 如果 home.php 也没有, 再回到当前主题目录下的index.php 文件,用它来生成页面。
Visual Overview 层次结构概览图
The following diagram shows which template files are called to generate a WordPress page based on the WordPress Template hierarchy.
下图显示了基于WordPress的模板层次结构,会调用哪个模板文件,生成WordPress页面。
(注:图中的框是根据查询字符串决定要请求的页面,左边的黑边白底框,然后跟随图中的箭头,灰框和深灰框,之后使用黑框)。
clip_image002
Full-Size Image (png)<-- 点击这里看下原图
A more in depth hierarchy diagram, including template-related conditional tags and body CSS classes, can be found here.
更深入的层次结构图,包括模板相关的条件标签,和HTML BODY元素的CSS类,可以在这?业健?模板层次细节
The following sections describe the order in which template files are being called by WordPress for each query type.
以下各节描述了,WordPress为每种查询类型,调用模板文件的顺序。
首页显示
Template file used to render the Blog Posts Index, whether on the site front page or on a static page. Note: on the Site Front Page, the Front Page template takes precedence over the Blog Posts Index (Home) template.
用于展现博客文章索引的模板文件,无论是在网站上首页或静态页的模板文件。注:对于站点首页,前端页模板优先于博客文章索引模板(首页)。1. home.php
2. index.php
Front Page display 前端页显示
Template file used to render the Site Front Page, whether the front page displays the Blog Posts Index or a static page. The Front Page template takes precedence over the Blog Posts Index (Home) template.
front-page.php - Used for both Your latest posts or A static page as set in the Front page displays section of Settings -> Reading
Page display rules - When Front page is set in the Front page displays section of Settings -> Reading
Home Page display rules - When Posts page is set in the Front page displays section of Settings -> Reading
Single Post display 单篇文章显示Template file used to render a single post page.
用于展示单个post页的文件。
single-{post_type}.php - If the post type were product, WordPress would look for single-product.php.
single.php
index.php
Page display Page显示
Template file used to render a static page (page post-type)
WordPress里面Page和Post是两个概念,可以自己查询一下。
custom template file - The Page Template assigned to the Page. See get_page_templates().
page-{slug}.php - If the page slug is recent-news, WordPress will look to use page-recent-news.php
page-{id}.php - If the page ID is 6, WordPress will look to use page-6.php
page.php
index.php
Category display 类别显示
Template file used to render a Category Archive Index page
category-{slug}.php - If the category's slug were news, WordPress would look for category-news.php
category-{id}.php - If the category's ID were 6, WordPress would look for category-6.php
category.php
archive.php
index.php
Tag display 标签显示
Template file used to render a Tag Archive Index page
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
Custom Taxonomies display 定制分类学显示
Template file used to render the Archive Index page for a Custom Taxonomy
taxonomy-{taxonomy}-{term}.php - If the taxonomy were sometax, and taxonomy's term were someterm WordPress would look for taxonomy-sometax-someterm.php. In the case of Post Formats, the taxonomy is 'post_format' and the terms are 'post-format-{format}. i.e. taxonomy-post_format-post-format-link.php
taxonomy-{taxonomy}.php - If the taxonomy were sometax, WordPress would look for taxonomy-sometax.php
taxonomy.php
archive.php
index.php
Custom Post Types display 定制文章类型显示
Template file used to render the Archive Index page for a Custom Post Type
archive-{post_type}.php - If the post type were product, WordPress would look for archive-product.php.
archive.php
index.php
(For rendering a single custom post type, refer to the Single Post display section above.)Author display 作者显示
Template file used to render an Author Archive Index page
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
Date display 日期显示
Template file used to render a Date-Based Archive Index page
date.php
archive.php
index.php
Search Result display 搜索结果显示
Template file used to render a Search Results Index page
search.php
index.php
404 (Not Found) display 404页面显示Template file used to render a Server 404 error page
1. 404.php
2. index.php
Attachment display 附件显示
Template file used to render a single attachment (attachment post-type) page
MIME_type.php - it can be any MIME type (image.php, video.php, application.php). For text/plain, in order:
text.php
plain.php
text_plain.php
attachment.php
single-attachment.php
single.php
index.php
Filter Hierarchy 过滤层次结构
The WordPress templates system allow you to filter the hierarchy. The filter (located in the get_query_template() function) uses this filter name: "{$type}_template" where $type is the a file name in the hierarchy without the .php extension.
WordPress的模板系统允许您筛选的层次结构(注:即更改默认查找模板的顺序,自己控制查找模板的顺序)。过滤器(位于get_query_template()函数)使用此过滤器的名称:
“{$type}_template”,这里$ type是类型层次结构中的文件名,不加.php扩展名。
Full list:
· index_template
· 404_template
· archive_template
· author_template
· category_template
· tag_template
· taxonomy_template
· date_template
· home_template
· front_page_template
· page_template
· paged_template
· search_template
· single_template
· text_template, plain_template, text_plain_template (all mime types)
· attachment_template
· comments_popup
举例
For example, let's take the default author hierarchy:
· author-{nicename}.php
· author-{id}.php
· author.php
To add author-{role}.php before author.php we can manipulate the actual hierarchy using the 'author_template' hook. This allows a request for /author/username where username has the role of editor to display using author-editor.php if present in the current themes directory.
要在模板调用顺序链中,author.php之前添加author-{role}.php ,我们可以使用author_template钩子 操纵实际的调用层次。这就可以处理形如 /author/username的请求,(该用户具有编辑角色),WordPress如果在当前主题目录中找到author-editor.php ,就调用显示它。
<?php
function author_role_template($templates = '')
{
$author = get_queried_object ();
$role = $author->roles [0];
if (! is_array ( $templates ) && ! empty ( $templates )) {
$templates = locate_template ( array (
"author-$role.php",
$templates
), false );
}
elseif (empty ( $templates )) {
$templates = locate_template ( "author-$role.php", false );
}
else {
$new_template = locate_template ( array (
"author-$role.php"
) );
if (! empty ( $new_template ))
array_unshift ( $templates, $new_template );
}
return $templates;
}
add_filter ( 'author_template', 'author_role_template' );
?>
本文标签:
很赞哦! ()
相关教程
图文教程
wordpress绑定多个域名的实现方法
使用过wordpress的站长应该都清楚,wordpress在安装的时候会默认绑定当前的域名,后续绑定其他的域名但是页面的链接也还是安装时候的域名。
wordpress实现多种语言切换的方法
对于wordpress多语言网站通常有两种形式, 头一种是独立的网站的多语言,另一种是多种语言但是独立的网站。
linux系统安装WordPress的方法
WordPress是一种使用PHP语言和MySQL数据库开发的开源、免费的Blog(博客,网志)引擎,用户可以在支持PHP和MySQL数据库的服务器上建立自己的Blog。WordPress是一个功能非常强大的博客系统
wordpress创建博客的步骤教程
首先下载WordPress安装包,这个直接到他们官网下载就可以了,百度就可以搜到,这里不方便放链接。
相关源码
-
自适应新闻资讯技术博客个人网站pbootcms模板该模板基于PbootCMS开源内核深度开发,该模板适用于游戏新闻网站、游戏博客等企业或个人网站,新闻资讯技术博客pbootcms模板;采用六级分辨率断点适配技术查看源码 -
(自适应响应式)宠物经验资讯咨询博客pbootcms网站源码下载除宠物资讯领域外,通过内容替换可快速适配宠物用品商城、宠物医疗咨询平台、宠物训练教程网站、动物保护组织官网、水族爱好者社区等垂直领域。查看源码 -
(自适应)电子元件电路板元器件pbootcms网站源码下载为电子元器件、电路板制造类企业设计,特别适合展示产品参数、技术规格等内容。采用响应式技术,确保各类电子元件在不同设备上都能清晰展示。查看源码 -
(自适应响应式)家电维修清晰服务网站pbootcms模板免费下载本模板基于PbootCMS内核开发,为维修服务类企业打造,特别适合家电维修、设备维护等行业使用。通过简洁直观的界面设计,帮助企业快速搭建专业级服务平台,实现线上业务高效管理。查看源码 -
(自适应)调节阀门气动球阀控制阀网站模板源码下载为调节阀门、气动球阀等工业设备企业打造的响应式网站模板,基于PbootCMS系统开发。突出产品参数展示与技术文档管理功能,通过专业化的布局设计有效呈现工业设备特性,适配各类终端访问需求。查看源码 -
快递物流公司pbootcms网站模板html响应式自适应源码下载基于HTML5+CSS3前沿技术开发,实现PC、平板、手机多端自适应。采用弹性布局与媒体查询技术,确保不同设备均有流畅视觉体验,企业形象统一。查看源码
| 分享笔记 (共有 篇笔记) |

