解析WordPress中的post_class與get_post_class函數(shù)
post_class()
post_class 是 WordPress 內(nèi)置的一個(gè)用于顯示文章 class 名稱的函數(shù),該函數(shù)通常會(huì)為每一篇文章生成獨(dú)一無二的 clss 值,如果你需要制作你自己的主題,而且還需要一點(diǎn)個(gè)性的話,那你最好駐足一下,使用該函數(shù)并配合靈活的 css 代碼,我們可以制作出個(gè)性化十足的 WordPress 博客。
post_class 函數(shù)描述
該函數(shù)通常會(huì)為每一篇文章生成獨(dú)一無二的 clss 值,可以很方便使用于文章所在的節(jié)點(diǎn)中。
函數(shù)使用
向其他的諸如 header_image、wp_title這樣的 WordPress 標(biāo)簽函數(shù)一樣,不帶 get 的函數(shù)通常是會(huì)直接顯示出來而不返回任何值。
<post id="post-<?php the_ID(); ?>" <?php post_class(); ?> > <?php the_content ;?> </post>
是的,也許你已經(jīng)注意到了,使用 post_class 函數(shù)時(shí)我們甚至不需要這樣去寫 clss=“post_class()”;。
實(shí)例結(jié)果
不賣關(guān)子,結(jié)果如下
<post id="post-888" class="post-888 post type-post status-publish format-standard hentry category-2 tag-wordpress" > 文章內(nèi)容 </post>
以使用為主的函數(shù)講完了,
下面照舊給出函數(shù)源代碼:
想要了解更多關(guān)于該函數(shù),以及get_post_class函數(shù)請(qǐng)關(guān)注后期文章。
/**
* 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 函數(shù)的基本實(shí)現(xiàn),在 WordPress 中其他一些帶 get 的函數(shù)一樣,該函數(shù)將會(huì)有一個(gè)返回值,而該返回值將是一個(gè)包含當(dāng)前文章基本信息的數(shù)組,get_post_class 函數(shù)主要用來給每篇文章生成獨(dú)一無為的 class 值而被封裝出來的。
如果你是一個(gè)要求不高的人的話,那么 post_class 這個(gè)函數(shù)其實(shí)已經(jīng)足夠你折騰了。如果你是一個(gè)有著精神潔癖的人,不想自己的 WordPress 網(wǎng)站有太多無用代碼的話,那你可以繼續(xù)往下看。
get_post_class函數(shù)詳解
該函數(shù)主要用來生成一個(gè)當(dāng)前文章相關(guān)信息的數(shù)組,該數(shù)組所含信息我們往往用來作為文章層中的 class 值。
就像我上面提到的 post_class 函數(shù),就是利用了本函數(shù)生成的 class 值。
并且該函數(shù)支持插入你自己的 class 值,一合并到返回?cái)?shù)組中。
以上是我本人的理解,當(dāng)然你也可以看一下官方的手冊(cè)。
比較費(fèi)解的手冊(cè)內(nèi)容如下:
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.
函數(shù)用法
<?php get_post_class($class, $post_id); ?>
如果在循環(huán)中,并且不需要插入自定義class值的話,該函數(shù)可不接受任何參數(shù)。
函數(shù)參數(shù)
$class:自定義 class 值,可以使字符串也可以死數(shù)組。
$post_id:文章ID
使用實(shí)例
$MyClass = get_post_class(); var_dump($MyClass);
輸出結(jié)果:
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"
}
進(jìn)階實(shí)例
$MyClass = get_post_class('index-post',249);
//或
$MyClass = get_post_class(array( 'index-post'),249);
var_dump($MyClass);
輸出結(jié)果:
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"
}
總結(jié)
根據(jù)函數(shù)的源代碼,我們可以看出,本函數(shù) class 值羅列的順序?yàn)椋?/p>
- 文章id
- 文章類型(頁(yè)面、文章)
- 文章類型(頁(yè)面、文章)與上一條相同,但結(jié)果中多了‘type-'字樣
- 發(fā)布狀態(tài)
- 文章格式
- 是否需要密碼
- 文章所述分類(會(huì)逐個(gè)羅列所述分類)
- 文章所述標(biāo)簽(會(huì)逐個(gè)羅列標(biāo)簽)
- WordPress開發(fā)中用于獲取近期文章的PHP函數(shù)使用解析
- WordPress開發(fā)中自定義菜單的相關(guān)PHP函數(shù)使用簡(jiǎn)介
- WordPress中用于獲取搜索表單的PHP函數(shù)使用解析
- 在WordPress中使用wp_count_posts函數(shù)來統(tǒng)計(jì)文章數(shù)量
- 詳解WordPress中調(diào)用評(píng)論模板和循環(huán)輸出評(píng)論的PHP函數(shù)
- 在WordPress中加入Google搜索功能的簡(jiǎn)單步驟講解
- 詳解WordPress開發(fā)中的get_post與get_posts函數(shù)使用
- WordPress開發(fā)中的get_post_custom()函數(shù)使用解析
- 在WordPress中安裝使用視頻播放器插件Hana Flv Player
- 詳解WordPress中分類函數(shù)wp_list_categories的使用
- WordPress開發(fā)中短代碼的實(shí)現(xiàn)及相關(guān)函數(shù)使用技巧
相關(guān)文章
PHP實(shí)現(xiàn)更新中間關(guān)聯(lián)表數(shù)據(jù)的兩種方法
這篇文章主要介紹了PHP實(shí)現(xiàn)更新中間關(guān)聯(lián)表數(shù)據(jù)的兩種方法,在進(jìn)行多表操作時(shí)比較有參考價(jià)值,需要的朋友可以參考下2014-09-09
原生JavaScript+PHP多圖上傳實(shí)現(xiàn)示例
這篇文章主要為大家介紹了原生JavaScript+PHP多圖上傳實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
解析dedeCMS驗(yàn)證碼的實(shí)現(xiàn)代碼
本篇文章是對(duì)dedeCMS驗(yàn)證碼的實(shí)現(xiàn)代碼進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
PHP封裝類似thinkphp連貫操作數(shù)據(jù)庫(kù)Db類與簡(jiǎn)單應(yīng)用示例
這篇文章主要介紹了PHP封裝類似thinkphp連貫操作數(shù)據(jù)庫(kù)Db類與簡(jiǎn)單應(yīng)用,涉及php基于mysqli的數(shù)據(jù)庫(kù)連接、增刪改查、異常處理等相關(guān)操作技巧,需要的朋友可以參考下2019-05-05
圖文詳解phpstorm配置Xdebug進(jìn)行調(diào)試PHP教程
這篇文章主要為大家詳細(xì)的介紹了phpstorm配置Xdebug進(jìn)行調(diào)試PHP教程 ,感興趣的小伙伴們可以參考一下2016-06-06
PHP實(shí)現(xiàn)的sqlite數(shù)據(jù)庫(kù)連接類
這篇文章主要介紹了PHP實(shí)現(xiàn)的sqlite數(shù)據(jù)庫(kù)連接類,涉及針對(duì)SQLite數(shù)據(jù)庫(kù)的連接與增刪改查等sql操作用法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2014-12-12
PHP實(shí)現(xiàn)根據(jù)瀏覽器跳轉(zhuǎn)不同語(yǔ)言頁(yè)面代碼
以下是對(duì)使用PHP實(shí)現(xiàn)根據(jù)瀏覽器跳轉(zhuǎn)不同語(yǔ)言頁(yè)面的代碼進(jìn)行了介紹,需要的朋友可以過來參考下2013-08-08
PHP中防止直接訪問或查看或下載config.php文件的方法
如何防止下載或訪問PHP的配置文件? 比如服務(wù)器里存放著config.php文件,里面有訪問數(shù)據(jù)庫(kù)的用戶名和密碼。怎么樣防止用戶查看該文件?2012-07-07
PHP+redis實(shí)現(xiàn)微博的推模型案例分析
這篇文章主要介紹了PHP+redis實(shí)現(xiàn)微博的推模型案例,結(jié)合實(shí)例形式分析了php+redis實(shí)現(xiàn)微博推送與關(guān)注功能相關(guān)操作技巧,需要的朋友可以參考下2019-07-07

