黄聪:走进wordpress 详细说说template-loader.php
再看template-laoder.php,這個(gè)文件總共只有45行。它的作用是基于訪問(wèn)的URL裝載正確的模板.
文件第六行,也是第一條語(yǔ)句,如下:
if ( defined('WP_USE_THEMES') && WP_USE_THEMES ) do_action('template_redirect');首先判斷是否使用Themes,這個(gè)WP_USE_THEMES常量在index.php中第一句就被設(shè)置為true。因此條件成立,會(huì)執(zhí)行do_action(‘template_redirect’)語(yǔ)句。
do_action('template_redirect')都做了什么?wordpress默認(rèn)的有如下:‘template_redirect’的Action(動(dòng)作)在include下的文件中出現(xiàn)。除了這里的do_action外,還有其他三個(gè)文 件中:default-filters.php,和ms-default-filters.php中以及canonical.php中出現(xiàn)。不包括wp- content目錄下面出現(xiàn)的。
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文件設(shè)置了wordpress大部分默認(rèn)的filter和action。文件內(nèi)容都是諸如下面的形式。
add_action( ‘xxx’,'xxxx’);
ms-default-filters.php是多站點(diǎn)時(shí)候設(shè)置的,內(nèi)容類似default-filters.php。wordpress默認(rèn)情況下沒(méi)有開(kāi)啟多站點(diǎn)。如果需要開(kāi)啟,請(qǐng)按照wordpress的指導(dǎo)文件操作。
add_action( 'template_redirect', 'wp_shortlink_header',11, 0)wp_shortlink_header位于文件link-template.php 2230行。 作用是如果當(dāng)前頁(yè)面定義了短連接,就在header中發(fā)送個(gè)短鏈接.形容: <link rel='shortlink' href='http://localhost/?p=1234' /> 這樣的縮短網(wǎng)址。 add_action( 'template_redirect', 'wp_old_slug_redirect');
wp_old_slug_redirect() 在query.php2807行,slug是什么?slug是url的一部分。在wordpress后臺(tái)的永久鏈接設(shè)置(/wp-admin /options-permalink.php)里,用戶可以自定義鏈接格式。絕大多數(shù)自定義的用戶喜歡在url中包含由文章標(biāo)題生成的一串字符,這一串 字符就是post slug。這個(gè)函數(shù)功能是重定向old slug到正確的鏈接。
接下來(lái)是個(gè)判斷語(yǔ)句;
// 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函數(shù)判斷當(dāng)前查詢是否是robot。位于query.php491行。
do_robots函數(shù)位于functions.php1779行。作用:顯示robot.txt的內(nèi)容。
?
然后是個(gè)大的if語(yǔ)句:
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;這個(gè)大if語(yǔ)句中形如get_xxxx_template()的函數(shù)都定義在theme.php中。
以get_index_template為例:作用在當(dāng)前或父模板下檢索index模板路徑,位置在theme.php725行。
function get_index_template() { return get_query_template('index'); }在這個(gè)函數(shù)里面,就把主題的模板給裝載進(jìn)來(lái)了,如何操作的?馬上來(lái)~~
轉(zhuǎn)載于:https://www.cnblogs.com/huangcong/p/4741046.html
新人創(chuàng)作打卡挑戰(zhàn)賽發(fā)博客就能抽獎(jiǎng)!定制產(chǎn)品紅包拿不停!總結(jié)
以上是生活随笔為你收集整理的黄聪:走进wordpress 详细说说template-loader.php的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Linux关于Sendmail配置错误的
- 下一篇: Centos6.4 编译安装 nginx