解析WordPress中的post_class与get_post_class函数


Posted in PHP onJanuary 04, 2016

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 相关文章推荐
[原创]效率较高的php下读取文本文件的代码
Jul 02 PHP
PHP 处理图片的类实现代码
Oct 23 PHP
PHP+Ajax异步通讯实现用户名邮箱验证是否已注册( 2种方法实现)
Dec 28 PHP
JS中encodeURIComponent函数用php解码的代码
Mar 01 PHP
根据中文裁减字符串函数的php代码
Dec 03 PHP
php禁止浏览器使用缓存页面的方法
Nov 07 PHP
php生成图片验证码-附五种验证码
Aug 19 PHP
实例讲解yii2.0在php命令行中运行的步骤
Dec 01 PHP
php使用正则验证中文
Apr 06 PHP
PHP使用mongoclient简单操作mongodb数据库示例
Feb 08 PHP
PHP session垃圾回收机制实例分析
Jun 28 PHP
Laravel 读取 config 下的数据方法
Oct 13 PHP
WordPress开发中的get_post_custom()函数使用解析
Jan 04 #PHP
在WordPress中安装使用视频播放器插件Hana Flv Player
Jan 04 #PHP
详解WordPress中分类函数wp_list_categories的使用
Jan 04 #PHP
大家须知简单的php性能优化注意点
Jan 04 #PHP
weiphp微信公众平台授权设置
Jan 04 #PHP
PHP在线书签系统分享
Jan 04 #PHP
PHP数据库连接mysql与mysqli对比分析
Jan 04 #PHP
You might like
回答PHPCHINA上的几个问题:URL映射
2007/02/14 PHP
PHP优于Node.js的五大理由分享
2012/09/15 PHP
解析ajax事件的调用顺序
2013/06/17 PHP
PHP发送短信代码分享
2015/08/11 PHP
PHP简单计算两个时间差的方法示例
2017/06/20 PHP
HTTP头隐藏PHP版本号实现过程解析
2020/12/09 PHP
jQuery实现鼠标可拖动调整表格列宽度
2014/05/26 Javascript
sogou地图API用法实例教程
2014/09/11 Javascript
javascript实现连续赋值
2015/08/10 Javascript
js实现改进的仿蓝色论坛导航菜单效果代码
2015/09/06 Javascript
微信小程序 教程之WXSS
2016/10/18 Javascript
Javascript 引擎工作机制详解
2016/11/30 Javascript
JavaScript之class继承_动力节点Java学院整理
2017/07/03 Javascript
angular实现spa单页面应用实例
2017/07/10 Javascript
js正则相关知识点专题
2018/05/10 Javascript
浅谈在vue中使用mint-ui swipe遇到的问题
2018/09/27 Javascript
[03:59]第二届DOTA2亚洲邀请赛选手传记-VGJ.rOtk
2017/04/03 DOTA
[01:05:30]VP vs TNC 2018国际邀请赛小组赛BO2 第一场 8.17
2018/08/20 DOTA
Windows下用py2exe将Python程序打包成exe程序的教程
2015/04/08 Python
python3序列化与反序列化用法实例
2015/05/26 Python
python入门:这篇文章带你直接学会python
2018/09/14 Python
Python3.4 splinter(模拟填写表单)使用方法
2018/10/13 Python
解决python打不开文件(文件不存在)的问题
2019/02/18 Python
python3读取autocad图形文件.py实例
2020/06/05 Python
在什么时候需要使用"常引用"
2015/12/31 面试题
铭立家具面试题
2012/12/06 面试题
化工工艺专业求职信
2013/09/22 职场文书
九年级体育教学反思
2014/01/23 职场文书
群众路线批评与自我批评
2014/02/06 职场文书
管理学院毕业生自荐信范文
2014/03/10 职场文书
党员干部三严三实心得体会
2014/10/13 职场文书
巾帼文明岗事迹材料
2014/12/24 职场文书
2015年法务工作总结范文
2015/05/23 职场文书
中国文明网2015年“向国旗敬礼”活动网上签名寄语
2015/09/24 职场文书
如何使用Python实现一个简易的ORM模型
2021/05/12 Python
正则表达式拆分url实例代码
2022/02/24 Java/Android