麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 開發 > PHP > 正文

解析WordPress中的post_class與get_post_class函數

2024-05-04 23:41:51
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了WordPress中的post_class與get_post_class函數,包括post_class()的PHP源碼的相應介紹,需要的朋友可以參考下
 

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

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

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

<post id="post-<?php the_ID(); ?>" <?php post_class(); ?> > <?php the_content ;?> </post>

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

實例結果
不賣關子,結果如下

<post id="post-888" class="post-888 post type-post status-publish format-standard hentry category-2 tag-wordpress" > 文章內容 </post>

以使用為主的函數講完了,
下面照舊給出函數源代碼:
想要了解更多關于該函數,以及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-'字樣
  • 發布狀態
  • 文章格式
  • 是否需要密碼
  • 文章所述分類(會逐個羅列所述分類)
  • 文章所述標簽(會逐個羅列標簽)


注:相關教程知識閱讀請移步到PHP教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 欧美国产综合视频 | 国产免费午夜 | 色婷婷久久一区二区 | 夜间福利网站 | 国产一级一国产一级毛片 | 2021免费日韩视频网 | 精品人伦一区二区三区蜜桃网站 | 大胆在线日本aⅴ免费视频 美国黄色毛片女人性生活片 | 在线观看中文字幕av | 精品无码久久久久久国产 | 成年人精品视频 | 国产精品区一区二区三区 | 精品久久久久久综合日本 | 国产免费一级淫片a级中文 99国产精品自拍 | 亚洲一区在线免费视频 | www亚洲| 国产一区二区三区四区五区加勒比 | 斗破苍穹在线免费 | 国产成人精品二区 | 中文字幕精品在线播放 | 日韩一级网站 | 男女羞羞视频在线免费观看 | 国产精品毛片va一区二区三区 | 国产精品久久久免费观看 | 亚洲一区二区三区日本久久九 | av在线播放电影 | 免费午夜视频在线观看 | 日韩在线播放中文字幕 | 成熟女人特级毛片www免费 | 日本欧美一区二区三区在线观看 | 久久久www视频 | 久久国产精品免费视频 | 狠狠干天天操 | 久久毛片免费观看 | 色蜜桃av | 狠狠操视频网站 | 国产精品免费久久久久久 | 91,视频免费看 | 成人三级在线播放 | 久久夜夜视频 | 欧美区在线 |