目錄
解析WordPress中的post_class与get_post_class函数,wordpresspostclass
您可能感兴趣的文章:
首頁 後端開發 php教程 解析WordPress中的post_class与get_post_class函数,wordpresspostclass_PHP教程

解析WordPress中的post_class与get_post_class函数,wordpresspostclass_PHP教程

Jul 12, 2016 am 09:01 AM
wordpress

解析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 的函数通常是会直接显示出来而不返回任何值。

<post id="post-<&#63;php the_ID(); &#63;>" <&#63;php post_class(); &#63;> > <&#63;php the_content ;&#63;> </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.
函数用法

<&#63;php get_post_class($class, $post_id); &#63;>
登入後複製

如果在循环中,并且不需要插入自定义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开发中短代码的实现及相关函数使用技巧

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1088784.htmlTechArticle解析WordPress中的post_class与get_post_class函数,wordpresspostclass post_class() post_class 是 WordPress 内置的一个用于显示文章 class 名称的函数,该函数通...
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

wordpress文章列表怎麼調 wordpress文章列表怎麼調 Apr 20, 2025 am 10:48 AM

有四種方法可以調整 WordPress 文章列表:使用主題選項、使用插件(如 Post Types Order、WP Post List、Boxy Stuff)、使用代碼(在 functions.php 文件中添加設置)或直接修改 WordPress 數據庫。

wordpress屏蔽ip的插件有哪些 wordpress屏蔽ip的插件有哪些 Apr 20, 2025 am 08:27 AM

WordPress 屏蔽 IP 的插件選擇至關重要。可考慮以下類型:基於 .htaccess:高效,但操作複雜;數據庫操作:靈活,但效率較低;基於防火牆:安全性能高,但配置複雜;自行編寫:最高控制權,但需要更多技術水平。

wordpress怎麼寫頁頭 wordpress怎麼寫頁頭 Apr 20, 2025 pm 12:09 PM

在WordPress中創建自定義頁頭的步驟如下:編輯主題文件“header.php”。添加您的網站名稱和描述。創建導航菜單。添加搜索欄。保存更改並查看您的自定義頁頭。

wordpress編輯日期怎麼取消 wordpress編輯日期怎麼取消 Apr 20, 2025 am 10:54 AM

WordPress 編輯日期可以通過三種方法取消:1. 安裝 Enable Post Date Disable 插件;2. 在 functions.php 文件中添加代碼;3. 手動編輯 wp_posts 表中的 post_modified 列。

wordpress主題頭部圖片如何更換 wordpress主題頭部圖片如何更換 Apr 20, 2025 am 10:00 AM

更換 WordPress 主題頭部圖片的分步指南:登錄 WordPress 儀錶盤,導航至“外觀”&gt;“主題”。選擇要編輯的主題,然後單擊“自定義”。打開“主題選項”面板並尋找“網站標頭”或“頭部圖片”選項。單擊“選擇圖像”按鈕並上傳新的頭部圖片。裁剪圖像並單擊“保存並裁剪”。單擊“保存並發布”按鈕以更新更改。

wordpress主機怎麼建站 wordpress主機怎麼建站 Apr 20, 2025 am 11:12 AM

要使用 WordPress 主機建站,需要:選擇一個可靠的主機提供商。購買一個域名。設置 WordPress 主機帳戶。選擇一個主題。添加頁面和文章。安裝插件。自定義您的網站。發布您的網站。

wordpress網站賬號登錄 wordpress網站賬號登錄 Apr 20, 2025 am 09:06 AM

登錄 WordPress 網站賬號的步驟:訪問登錄頁面:輸入網站網址加上 "/wp-login.php"。輸入用戶名和密碼。點擊“登錄”。驗證兩步驗證(可選)。成功登錄後,您將看到網站儀錶盤。

wordpress評論怎麼顯示 wordpress評論怎麼顯示 Apr 20, 2025 pm 12:06 PM

WordPress 網站中啟用評論功能:1. 登錄管理面板,轉到 "設置"-"討論",勾選 "允許評論";2. 選擇顯示評論的位置;3. 自定義評論表單;4. 管理評論,批准、拒絕或刪除;5. 使用 &lt;?php comments_template(); ?&gt; 標籤顯示評論;6. 啟用嵌套評論;7. 調整評論外形;8. 使用插件和驗證碼防止垃圾評論;9. 鼓勵用戶使用 Gravatar 頭像;10. 創建評論指

See all articles