黄聪:走进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'); }在這個函數里面,就把主題的模板給裝載進來了,如何操作的?馬上來~~
轉載于:https://www.cnblogs.com/huangcong/p/4741046.html
新人創作打卡挑戰賽發博客就能抽獎!定制產品紅包拿不停!總結
以上是生活随笔為你收集整理的黄聪:走进wordpress 详细说说template-loader.php的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux关于Sendmail配置错误的
- 下一篇: Linux 下 -bash: mysql