再看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'); }

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

黄聪:走进wordpress 详细说说template-loader.php的更多相关文章

  1. 黄聪:《跟黄聪学WordPress插件开发》

    续<跟黄聪学WordPress主题开发>之后,又一个作品完成!<跟黄聪学Wordpress插件开发>,国内最好的Wordpress插件开发视频教程!! 目录预览: WordPr ...

  2. 黄聪:《跟黄聪学WordPress主题开发》

    又一个作品完成!<跟黄聪学Wordpress主题开发>,国内最好的Wordpress主题模版开发视频教程!! 目录预览: WordPress官方源文件层式结构讲解 WordPress数据库 ...

  3. 黄聪:wordpress教程

    又一个作品完成!<跟黄聪学Wordpress主题开发>,国内最好的Wordpress主题模版开发视频教程!! 目录预览: WordPress官方源文件层式结构讲解 WordPress数据库 ...

  4. 黄聪:wordpress工作原理

    WP初始化的过程:当你输入<yourlink>/wordpress对wordpress进行初始化时,wordpress默认会找根目录下的index.php页面,看一下index.php页面 ...

  5. 黄聪:Wordpress 模版技术手册 - WordPress Theme Technical manuals

    WordPress基本模板文件 一套完整的WordPress模板应至少具有如下文件: style.css : CSS(样式表)文件 index.php : 主页模板 archive.php : Arc ...

  6. 黄聪:wordpress源码解析-数据库表结构(转)

    如果是一个普通的用户,不需要了解wordpress数据库的结构.但是,如果你正在写一个插件,你应该会对wordpress如何处理它的数据和关系感兴趣.如果你已经尝试使用已经存在的wordpress a ...

  7. 黄聪:wordpress中remove_action、add_action、 do_action()的hook钩子都有哪些

    原文地址:http://codex.wordpress.org/Plugin_API/Action_Reference muplugins_loaded After must-use plugins ...

  8. 黄聪:WordPress 多站点建站教程(四):获取子站点相关信息(站点的注册时间,修改时间,总文章数,URL等)

    1.获取子站点blogs表里面的内容信息 $blog_details = get_blog_details(1); echo 'Blog '.$blog_details->blog_id.' i ...

  9. 黄聪:Wordpress二级域名共享用户cookie出现错误解决方案及WP的Cookie机制

    在若干年以前,我刚开始折腾Wordpress没多久的时候,就自己摸索过 多个Wordpress网站共享一份数据表的实现方法 .这种看起来好像很高大上的类SSO功能,能够给用户在多个网站之间提供快速.无 ...

随机推荐

  1. OC小结

    #import <Foundation/Foundation.h>#import "Person.h"int main(int argc, const char * a ...

  2. tyvj1023 - 奶牛的锻炼 ——DP

    题目链接:https://www.tyvj.cn/Problem_Show.aspx?id=1023 #include <cstdio> #include <algorithm> ...

  3. c 深度剖析 6

    函数的编码风格 1.注释 2,空行 3,缩进 4,参数长度,代码长度,语句长度要合适. 5,少用全局变量 6,指针仅作输入参数时,可用const 设置其为只读属性,避免其在函数中被修改. 7,函数默认 ...

  4. hihoCoder#1055 : 刷油漆 (树形DP+01背包)

    题目大意:给一棵带点权的树,现在要从根节点开始选出m个连通的节点,使总权值最大. 题目分析:定义状态dp(u,m)表示在以u为根的子树从根节点开始选出m个点连通的最大总权值,则dp(u,m)=max( ...

  5. [AHOI 2006][BZOJ 1269]文本编辑器editor

    好吧,我承认这是我用来刷随笔数的喵~ 这是一道 splay 裸题,但还是有想本傻 X 一样根本不会写 splay 的,于是乎又用 treap 水过了 splay 的常数我还是知道的,所以真是不知道那些 ...

  6. Ubuntu 14.04 关于 TensorFlow 环境的配置

    Ubuntu 14.04 关于 TensorFlow 环境的配置   本教程截图于 TensorFlow 官方文档中文版  https://github.com/jikexueyuanwiki/ten ...

  7. 纯CSS绘制三角形(各种角度)

    我们的网页因为 CSS 而呈现千变万化的风格.这一看似简单的样式语言在使用中非常灵活,只要你发挥创意就能实现很多比人想象不到的效果.特别是随着 CSS3 的广泛使用,更多新奇的 CSS 作品涌现出来. ...

  8. python扫描内网banner信息

    小菜自己无聊写着玩,主要纪念以前的逗逼学习,可以改IPy import mechanize import cookielib import socket import argparse import ...

  9. cocos2dx 2.x 在ios8下clippingNode不起作用 解决办法

    升级xcode到6.1后,跑以前的cocos2dx 2.x写的项目,发现clippingNode失效了. 后来看到这个帖子,解决了我的问题:http://discuss.cocos2d-x.org/t ...

  10. expression encoder 4 安装 出现“已经安排重启您的计算机

    问题: expression encoder 4  安装 出现“已经安排重启您的计算机 解决的办法,注册表数据的修改 开始 运行 regedit HKEY_LOCAL_MACHINE\SYSTEM\C ...