深入解析WordPress中載入模板的get_template_part函數,wordpresstemplate_PHP教程

來源:互聯網
上載者:User

深入解析WordPress中載入模板的get_template_part函數,wordpresstemplate


最近研究官方主題 Twenty Eleven ,有一些東西網上現成的中文資料不好找,在部落格裡記載下來,算是分享,也算是備忘,wordpress 3.0 以後就開始便有了get_template_part() 這個函數 ,應該是為文章呈現形式提供更為多樣化的選擇而給出的新功能。

Twenty Eleven 中 執行個體如下:

Twenty Eleven index.php 檔案
行:21

<?php if ( have_posts() ) : ?> <?php twentyeleven_content_nav( 'nav-above' ); ?> <?php /* Start the Loop 在迴圈中使用以調用不同類型的文章 */ ?> <?php while ( have_posts() ) : the_post(); ?> <?php get_template_part( 'content', get_post_format() ); ?> <?php endwhile; ?>............................<?php endif; ?>

描述:
載入一個制定的模板到另一個模板裡面(不同於包含header,sidebar,footer).
使得一個主題使用子模板來實現程式碼片段重用變得簡單

用於在模板中包含指定的模板檔案,只需用指定參數slug和name就可以包含檔案{slug}-{name}.php,最重要的功能是如果沒有這個檔案就包含沒有{name}的.php檔案檔案

使用方法:

<?php get_template_part( $slug, $name ) ?>

參數:

  • $slug (必須) 通用的模板名
  • $name (可選) 指定的模板名

樣本:

使用 loop.php 在子主題裡面

假設主題檔案夾wp-content/themes下父主題是twentyten子主題twentytenchild,那麼下面的代碼:

<?php get_template_part( 'loop', 'index' ); ?>

php 的require()函數將按下面優先順序包含檔案

1. wp-content/themes/twentytenchild/loop-index.php
2. wp-content/themes/twentytenchild/loop.php
3. wp-content/themes/twentyten/loop-index.php
4. wp-content/themes/twentyten/loop.php

導航(這個例子很爛,但卻是另一種使用思路)
使用通用的nav.php檔案給主題添加導航條:

<?php get_template_part( 'nav' );      // Navigation bar (nav.php) ?><?php get_template_part( 'nav', '2' );   // Navigation bar #2 (nav-2.php) ?><?php get_template_part( 'nav', 'single' ); // Navigation bar to use in single pages (nav-single.php) ?>

get_template_part() 的鉤子詳解
因為在官方主題(Twenty Eleven)中 get_template_part() 函數被大量使用,所以就目前來看,該函數應該算是比較熱門的一個函數了,之前有寫過一篇文章講述該函數的具體使用方法,在這裡也就不便再贅述,本文主要針對該函數的 add_action 中的 hook $tag 值進行探討,因為,WP hook 中林林總總有那麼些函數在$tag 值中比較讓人費解。

與普通hook的區別
普通的hook的$tag 是一個固定值,而 get_template_part() 確是一個可變值,好吧先不說,wp這麼做給我們實現一個簡易功能帶來多少麻煩,但如此設定確實給多樣化的主題實現帶來了不少方便之處。
實現這一原理的原始碼如下,截取自 WordPress 來源程式。

 function get_template_part( $slug, $name = null ) {//$tag = "get_template_part_{$slug}" //也就是,get_template_part_+你當時設定的$slug值 do_action( "get_template_part_{$slug}", $slug, $name ); $templates = array(); if ( isset($name) )  $templates[] = "{$slug}-{$name}.php";  $templates[] = "{$slug}.php";  locate_template($templates, true, false);}

執行個體
像上面那樣說,可能也許基本上有點看不明白,好吧給點執行個體

//複習一下get_template_part($slug, $name)的用法,//如果你在主題裡這樣get_template_part( 'index' , 'photo');//那麼 WP 會去找主題根目錄下 index-photo.php 檔案 //那麼我們想掛一個函數的話就得像如下function addFunction ($slug, $name){echo $slug;}add_action("get_template_part_index","addFunction",10,2);

get_template_part() 函數詳解備忘

您可能感興趣的文章:

  • 詳解WordPress中調用評論模板和迴圈輸出評論的PHP函數
  • WordPress中擷取所使用的模板的頁面ID的簡單方法
  • WordPress主題製作之模板檔案的引入方法
  • WordPress的主題編寫中擷取頭部模板和底部模板
  • 講解WordPress中用於擷取評論模板和搜尋表單的PHP函數

http://www.bkjia.com/PHPjc/1091104.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1091104.htmlTechArticle深入解析WordPress中載入模板的get_template_part函數,wordpresstemplate 最近研究官方主題 Twenty Eleven ,有一些東西網上現成的中文資料不好找,在...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.