走進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`); }
在這個函式裡面,就把主題的模板給裝載進來了,如何操作的?馬上來~~
本文轉自黃聰部落格園部落格,原文連結:http://www.cnblogs.com/huangcong/p/4741046.html,如需轉載請自行聯絡原作者
相關文章
- 【Docker】(10)---詳細說說 Dockerfile檔案Docker
- nginx 詳解 - 詳細配置說明Nginx
- nginx 詳解 – 詳細配置說明Nginx
- Linux sed命令詳細說明Linux
- 面試官:你說你精通 Docker,那你來詳細說說 Dockerfile 吧面試Docker
- winscp操作說明,winscp操作說明的詳細解讀
- VNC安裝配置詳細說明VNC
- Flask-Limit使用詳細說明FlaskMIT
- jpa 方法 命名規則 詳細說明
- 細說WebServiceWeb
- JQuery Datatables Columns API 引數詳細說明jQueryAPI
- 寬頻路由器的詳細說明路由器
- 面試官:實戰中用過CountDownLatch嗎?詳細說一說,我:啊這面試CountDownLatch
- 安裝wordpress教程詳細安裝wordpress步揍
- 細說ThreadLocal(一)thread
- 整合測試——Spring TestContext框架支援詳細解說SpringContext框架
- java 數字金字塔,含詳細解說Java
- Python Matplotlib add_subplot 和 subplots_adjust詳解及程式碼詳細說明 配圖片說明Python
- 細說event-loopOOP
- NSArray 簡單細說
- 細說HTTP增量更新HTTP
- 細說三極體
- 每日一問:詳細說一下 MeasureSpec.UNSPECIFIED
- zblog模板文章釋出時間格式詳細說明
- Thread interrupt() 執行緒中斷的詳細說明thread執行緒
- 詳細說明搜尋引擎優化的過程優化
- 帶你走進《天穗之咲稻姬》裡說了與沒說的種田“祕聞”(下)
- 帶你走進《天穗之咲稻姬》裡說了與沒說的種田“祕聞”(上)
- C++類開發第七篇(詳細說說多型和編譯原理)C++多型編譯原理
- Centos7安裝WordPress詳細教程CentOS
- linux安裝mysql的步驟和方法詳細說明LinuxMySql
- 網路交換機功能和原理的詳細說明
- golang基礎–細說deferGolang
- 細說 Golang 的 JSON 解析GolangJSON
- 細說Redis分散式鎖?Redis分散式
- 細說白盒測試
- 前端meta標籤內容定義及使用說明,meta詳細說明,meta標籤使用前端
- 最詳細的解說—時間和空間複雜度複雜度
- Spring 對於事務上的應用的詳細說明Spring