您现在的位置是:首页 > cms教程 > WordPress教程WordPress教程
WordPress模板层次结构分析
冰蓝2025-03-17 22:52:42WordPress教程已有2人查阅
导读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在当前主题的目录使用特定的名称(注:这会遵循一个从特殊到一般的过程)查找模板文件,并使用第一个匹配的模板文件,这个查找查询相关的模板文件的过程会在后面列出。 除了基本的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为每种查询类型,调用模板文件的顺序。
首页显示 用于展现博客文章索引的模板文件,无论是在网站上首页或静态页的模板文件。注:对于站点首页,前端页模板优先于博客文章索引模板(首页)。
1. home.php
2. index.php
Front Page display 前端页显示 Single Post display 单篇文章显示
Template file used to render a single post page.
用于展示单个post页的文件。 Page display Page显示 WordPress里面Page和Post是两个概念,可以自己查询一下。 Category display 类别显示 Tag display 标签显示 Custom Taxonomies display 定制分类学显示 Custom Post Types display 定制文章类型显示 (For rendering a single custom post type, refer to the Single Post display section above.)
Author display 作者显示 Date display 日期显示 Search Result display 搜索结果显示 404 (Not Found) display 404页面显示
Template file used to render a Server 404 error page
1. 404.php
2. index.php
Attachment display 附件显示 Filter Hierarchy 过滤层次结构 WordPress的模板系统允许您筛选的层次结构(注:即更改默认查找模板的顺序,自己控制查找模板的顺序)。过滤器(位于get_query_template()函数)使用此过滤器的名称: 举例 要在模板调用顺序链中,author.php之前添加author-{role}.php ,我们可以使用author_template钩子 操纵实际的调用层次。这就可以处理形如 /author/username的请求,(该用户具有编辑角色),WordPress如果在当前主题目录中找到author-editor.php ,就调用显示它。
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在当前主题的目录使用特定的名称(注:这会遵循一个从特殊到一般的过程)查找模板文件,并使用第一个匹配的模板文件,这个查找查询相关的模板文件的过程会在后面列出。 除了基本的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为每种查询类型,调用模板文件的顺序。
首页显示 用于展现博客文章索引的模板文件,无论是在网站上首页或静态页的模板文件。注:对于站点首页,前端页模板优先于博客文章索引模板(首页)。
1. home.php
2. index.php
Front Page display 前端页显示 Single Post display 单篇文章显示
Template file used to render a single post page.
用于展示单个post页的文件。 Page display Page显示 WordPress里面Page和Post是两个概念,可以自己查询一下。 Category display 类别显示 Tag display 标签显示 Custom Taxonomies display 定制分类学显示 Custom Post Types display 定制文章类型显示 (For rendering a single custom post type, refer to the Single Post display section above.)
Author display 作者显示 Date display 日期显示 Search Result display 搜索结果显示 404 (Not Found) display 404页面显示
Template file used to render a Server 404 error page
1. 404.php
2. index.php
Attachment display 附件显示 Filter Hierarchy 过滤层次结构 WordPress的模板系统允许您筛选的层次结构(注:即更改默认查找模板的顺序,自己控制查找模板的顺序)。过滤器(位于get_query_template()函数)使用此过滤器的名称: 举例 要在模板调用顺序链中,author.php之前添加author-{role}.php ,我们可以使用author_template钩子 操纵实际的调用层次。这就可以处理形如 /author/username的请求,(该用户具有编辑角色),WordPress如果在当前主题目录中找到author-editor.php ,就调用显示它。
本文标签:
很赞哦! (0)
暂无内容 |
暂无内容 |
相关文章
暂无内容 |
暂无内容 |
随机图文
wordpress实现文章分页的方法
wordpress怎么实现文章分页?由于篇幅过长导致页面加载速度过慢,降低用户体验,所以必须要找个方法把长文章进行分页,搜索了一下,发现有个代码解决的方法非常好。WordPress站点文章排序修改方法
我们常见的WordPress站点文章排序,通常情况下是按发布时间的升序或降序方式排列,这种排序方式几乎可以满足大多数站点的需求了,但是有些另类的站点可能会有一些另类的需求Centos搭建wordpress网站的方法
1、装apache、mariadbyum install httpd mariadb-server php php-mysql -ysystemctl start httpdsystemctl enable httpdsystemctl start mariadbsystemctl enable marwordpress后台密码怎么改,wordpress后台密码修改方法
打开浏览器,进入自己要修改的网站后台地址。在登录框内输入网站后台的账户和密码后,点击进入。
留言与评论 (共有 0 条评论) |