设为首页 收藏本站
查看: 1085|回复: 0

[经验分享] 走进wordpress 详细说说template-loader.php

[复制链接]
累计签到:2 天
连续签到:1 天
发表于 2015-8-29 03:12:35 | 显示全部楼层 |阅读模式
  再看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'); }  在这个函数里面,就把主题的模板给装载进来了,如何操作的?马上来~~

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-105642-1-1.html 上篇帖子: PHP中的Session问题 下篇帖子: PHP调试环境
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表