解析WordPress中的post_class與get_post_class函數,wordpresspostclass_PHP教程

來源:互聯網
上載者:User

解析WordPress中的post_class與get_post_class函數,wordpresspostclass


post_class()
post_class 是 WordPress 內建的一個用於顯示文章 class 名稱的函數,該函數通常會為每一篇文章產生獨一無二的 clss 值,如果你需要製作你自己的主題,而且還需要一點個性的話,那你最好駐足一下,使用該函數並配合靈活的 css 代碼,我們可以製作出個人化十足的 WordPress 部落格。

post_class 函數描述
該函數通常會為每一篇文章產生獨一無二的 clss 值,可以很方便使用於文章所在的節點中。

函數使用
向其他的諸如 header_image、wp_title這樣的 WordPress 標籤函數一樣,不帶 get 的函數通常是會直接顯示出來而不返回任何值。

 > <?php the_content ;?> 

是的,也許你已經注意到了,使用 post_class 函數時我們甚至不需要這樣去寫 clss=“post_class()”;。

執行個體結果
不賣關子,結果如下

 文章內容 

以使用為主的函數講完了,
下面照舊給出函數原始碼:
想要瞭解更多關於該函數,以及get_post_class函數請關注後期文章。

/** * Display the classes for the post div. * * @since 2.7.0 * * @param string|array $class One or more classes to add to the class list. * @param int $post_id An optional post ID. */function post_class( $class = '', $post_id = null ) { // Separates classes with a single space, collates classes for post DIV echo 'class="' . join( ' ', get_post_class( $class, $post_id ) ) . '"';}

get_post_class 詳解
get_post_class 是 post_class 函數的基本實現,在 WordPress 中其他一些帶 get 的函數一樣,該函數將會有一個傳回值,而該傳回值將是一個包含當前文章基本資料的數組,get_post_class 函數主要用來給每篇文章產生獨一無為的 class 值而被封裝出來的。

如果你是一個要求不高的人的話,那麼 post_class 這個函數其實已經足夠你折騰了。如果你是一個有著精神潔癖的人,不想自己的 WordPress 網站有太多無用代碼的話,那你可以繼續往下看。

get_post_class函數詳解
該函數主要用來產生一個當前文章相關資訊的數組,該數組所含資訊我們往往用來作為文章層中的 class 值。
就像我上面提到的 post_class 函數,就是利用了本函數產生的 class 值。
並且該函數支援插入你自己的 class 值,一合并到返回數組中。
以上是我本人的理解,當然你也可以看一下官方的手冊。

比較費解的手冊內容如下:
WordPress Themes have a template tag for the post HMTL tag which will help theme authors to style more effectively with CSS. The Template Tag is called get_post_class. This function returns different post container classes which can be added, typically, in the index.php, single.php, and other template files featuring post content, typically in the HTML

tag.
函數用法

<?php get_post_class($class, $post_id); ?>

如果在迴圈中,並且不需要插入自訂class值的話,該函數可不接受任何參數。

函數參數
$class:自訂 class 值,可以使字串也可以死數組。

$post_id:文章ID

使用執行個體

$MyClass = get_post_class();  var_dump($MyClass);

輸出結果:

array(9) { [0]=> string(8) "post-249" [1]=> string(4) "post" [2]=> string(9) "type-post" [3]=> string(14) "status-publish" [4]=> string(15) "format-standard" [5]=> string(6) "hentry" [6]=> string(18) "category-catcatcat" [7]=> string(8) "tag-tag1" [8]=> string(8) "tag-tag2"}

進階執行個體

$MyClass = get_post_class('index-post',249);//或 $MyClass = get_post_class(array( 'index-post'),249); var_dump($MyClass);

輸出結果:

array(10) { [0]=> string(8) "post-249" [1]=> string(4) "post" [2]=> string(9) "type-post" [3]=> string(14) "status-publish" [4]=> string(15) "format-standard" [5]=> string(6) "hentry" [6]=> string(18) "category-catcatcat" [7]=> string(8) "tag-tag1" [8]=> string(8) "tag-tag2" [9]=> string(10) "index-post"}

總結
根據函數的原始碼,我們可以看出,本函數 class 值羅列的順序為:

  • 文章id
  • 文章類型(頁面、文章)
  • 文章類型(頁面、文章)與上一條相同,但結果中多了‘type-'字樣
  • 發布狀態
  • 文章格式
  • 是否要求輸入密碼
  • 文章所述分類(會逐個羅列所述分類)
  • 文章所述標籤(會逐個羅欄標籤)

您可能感興趣的文章:

  • WordPress開發中用於擷取近期文章的PHP函數使用解析
  • WordPress開發中自訂菜單的相關PHP函數使用簡介
  • WordPress中用於擷取搜尋表單的PHP函數使用解析
  • 在WordPress中使用wp_count_posts函數來統計文章數量
  • 詳解WordPress中調用評論模板和迴圈輸出評論的PHP函數
  • 在WordPress中加入Google搜尋功能的簡單步驟講解
  • 詳解WordPress開發中的get_post與get_posts函數使用
  • WordPress開發中的get_post_custom()函數使用解析
  • 在WordPress中安裝使用視頻播放器外掛程式Hana Flv Player
  • 詳解WordPress中分類函數wp_list_categories的使用
  • WordPress開發中短代碼的實現及相關函數提示

http://www.bkjia.com/PHPjc/1088784.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1088784.htmlTechArticle解析WordPress中的post_class與get_post_class函數,wordpresspostclass post_class() post_class 是 WordPress 內建的一個用於顯示文章 class 名稱的函數,該函數通...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.