当前位置: 首页 > news >正文

走进wordpress 详细说说template-loader.php

再看template-laoder.php,这个文件总共只有45行。它的作用是基于访问的URL装载正确的模板.

文件第六行,也是第一条语句,如下:

if ( defined('WP_USE_THEMES') && WP_USE_THEMES )     do_action('template_redirect');

首先判断是否使用Themes,这个WP_USE_THEMES常量在index.php中第一句就被设置为true。因此条件成立,会执行do_action(‘template_redirect’)语句。

do_action('template_redirect')都做了什么?wordpress默认的有如下:

‘template_redirect’的Action(动作)在include下的文件中出现。除了这里的do_action外,还有其他三个文 件中:default-filters.php,和ms-default-filters.php中以及canonical.php中出现。不包括wp- content目录下面出现的。

canonical.php (411行):  add_action(‘template_redirect’, ‘redirect_canonical’);

default-filters.php(200行): add_action( ‘template_redirect’,   ‘wp_shortlink_header’,11, 0 );

default-filters.php(243行):  add_action( ‘template_redirect’, ‘wp_old_slug_redirect’);

ms-default-filters.php(32行):  add_action( ‘template_redirect’, ‘maybe_redirect_404′ );

ms-functions.php(1353行):  add_action( ‘template_redirect’, ‘redirect_mu_dashboard’ );

default-filters.php文件设置了wordpress大部分默认的filter和action。文件内容都是诸如下面的形式。

add_action( ‘xxx’,'xxxx’);

ms-default-filters.php是多站点时候设置的,内容类似default-filters.php。wordpress默认情况下没有开启多站点。如果需要开启,请按照wordpress的指导文件操作。

add_action( 'template_redirect',   'wp_shortlink_header',11, 0)




wp_shortlink_header位于文件link-template.php 2230行。
作用是如果当前页面定义了短连接,就在header中发送个短链接.形容:
<link rel='shortlink' href='http://localhost/?p=1234' />
这样的缩短网址。
add_action( 'template_redirect', 'wp_old_slug_redirect');

wp_old_slug_redirect() 在query.php2807行,slug是什么?slug是url的一部分。在wordpress后台的永久链接设置(/wp-admin /options-permalink.php)里,用户可以自定义链接格式。绝大多数自定义的用户喜欢在url中包含由文章标题生成的一串字符,这一串 字符就是post slug。这个函数功能是重定向old slug到正确的链接。

接下来是个判断语句;

// Process feeds and trackbacks even if not using themes. if ( is_robots() ) :     do_action('do_robots');     return; elseif ( is_feed() ) :     do_feed();     return; elseif ( is_trackback() ) :     include( ABSPATH . 'wp-trackback.php' );     return; endif;

is_robots函数判断当前查询是否是robot。位于query.php491行。

do_robots函数位于functions.php1779行。作用:显示robot.txt的内容。

 

然后是个大的if语句:

if ( defined('WP_USE_THEMES') && WP_USE_THEMES ) :     $template = false;     if     ( is_404()   && $template = get_404_template() ) :     elseif ( is_search() && $template = get_search_template()) :     elseif ( is_tax() && $template = get_taxonomy_template()) :     elseif ( is_front_page() && $template = get_front_page_template()) :     elseif ( is_home() && $template = get_home_template()) :     elseif ( is_attachment() && $template = get_attachment_template()) :         remove_filter('the_content', 'prepend_attachment');     elseif ( is_single() && $template = get_single_template()) :     elseif ( is_page() && $template = get_page_template()) :     elseif ( is_category() && $template = get_category_template()) :     elseif ( is_tag()&& $template = get_tag_template()) :     elseif ( is_author()&& $template = get_author_template()) :     elseif ( is_date()&& $template = get_date_template()) :     elseif ( is_archive()&& $template = get_archive_template()) :     elseif ( is_comments_popup() && $template = get_comments_popup_template() ) :     elseif ( is_paged()&& $template = get_paged_template()          ) :     else :         $template = get_index_template();     endif;      if ( $template = apply_filters( 'template_include', $template ) )         include( $template );     return; endif;

这个大if语句中形如get_xxxx_template()的函数都定义在theme.php中。

以get_index_template为例:作用在当前或父模板下检索index模板路径,位置在theme.php725行。

function get_index_template() { 	return get_query_template('index'); }

在这个函数里面,就把主题的模板给装载进来了,如何操作的?马上来~~

相关文章:

  • ubuntu的备份和还原
  • 【转】Android 带checkbox的listView 实现多选,全选,反选----解决checkbox错位问题
  • ACL访问控制列表及特殊权限
  • [裴礼文数学分析中的典型问题与方法习题参考解答]4.5.5
  • Ubuntu APT使用指南
  • 6个常见的 PHP 安全性攻击
  • new/delete malloc/free深入剖析
  • Linux下使用locale命令设置语言环境
  • EOS的综合除尘系统
  • 树状数组 POJ 2481 Cows
  • 匿名内部类
  • HDU1664 BFS + 数论 + 剪枝
  • HP VA7400存储故障诊断,数据恢复有可能
  • 对创业的反思-自我定位
  • UITableView 基本使用方法总结
  • [译]Python中的类属性与实例属性的区别
  • Eureka 2.0 开源流产,真的对你影响很大吗?
  • Hibernate最全面试题
  • HTTP请求重发
  • java2019面试题北京
  • JavaScript创建对象的四种方式
  • Java面向对象及其三大特征
  • Laravel核心解读--Facades
  • Redis在Web项目中的应用与实践
  • 从tcpdump抓包看TCP/IP协议
  • 极限编程 (Extreme Programming) - 发布计划 (Release Planning)
  • 将回调地狱按在地上摩擦的Promise
  • 快速体验 Sentinel 集群限流功能,只需简单几步
  • 每个JavaScript开发人员应阅读的书【1】 - JavaScript: The Good Parts
  • 前端面试之CSS3新特性
  • 前端性能优化——回流与重绘
  • 视频flv转mp4最快的几种方法(就是不用格式工厂)
  • 微信公众号开发小记——5.python微信红包
  • 微信小程序--------语音识别(前端自己也能玩)
  • LevelDB 入门 —— 全面了解 LevelDB 的功能特性
  • SAP CRM里Lead通过工作流自动创建Opportunity的原理讲解 ...
  • 新海诚画集[秒速5センチメートル:樱花抄·春]
  • ​LeetCode解法汇总518. 零钱兑换 II
  • ​ssh免密码登录设置及问题总结
  • # Apache SeaTunnel 究竟是什么?
  • #define用法
  • (51单片机)第五章-A/D和D/A工作原理-A/D
  • (6)【Python/机器学习/深度学习】Machine-Learning模型与算法应用—使用Adaboost建模及工作环境下的数据分析整理
  • (C++)栈的链式存储结构(出栈、入栈、判空、遍历、销毁)(数据结构与算法)
  • (Pytorch框架)神经网络输出维度调试,做出我们自己的网络来!!(详细教程~)
  • (ZT) 理解系统底层的概念是多么重要(by趋势科技邹飞)
  • (超详细)2-YOLOV5改进-添加SimAM注意力机制
  • (独孤九剑)--文件系统
  • (二)斐波那契Fabonacci函数
  • (剑指Offer)面试题34:丑数
  • (已更新)关于Visual Studio 2019安装时VS installer无法下载文件,进度条为0,显示网络有问题的解决办法
  • .MyFile@waifu.club.wis.mkp勒索病毒数据怎么处理|数据解密恢复
  • .NET Core 中的路径问题
  • .Net Core与存储过程(一)
  • .NET MVC第五章、模型绑定获取表单数据