解析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 存储文本换行实现方法
Jan 05 PHP
《PHP编程最快明白》第五讲:php目录、文件操作
Nov 01 PHP
DISCUZ在win2003环境下 Unable to access ./include/common.inc.php in... 的问题终极解决方案
Nov 21 PHP
php实现的DateDiff和DateAdd时间函数代码分享
Aug 16 PHP
PHP表单数据写入MySQL数据库的代码
May 31 PHP
详解PHP中cookie和session的区别及cookie和session用法小结
Jun 12 PHP
php中strlen和mb_strlen用法实例分析
Nov 12 PHP
PHP 500报错的快速解决方法
Dec 14 PHP
利用laravel+ajax实现文件上传功能方法示例
Aug 13 PHP
PHP封装mysqli基于面向对象的mysql数据库操作类与用法示例
Feb 25 PHP
Thinkphp5框架ajax接口实现方法分析
Aug 28 PHP
thinkphp5.1 框架导入/导出excel文件操作示例
May 25 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
php中用文本文件做数据库的实现方法
2008/03/27 PHP
php Smarty date_format [格式化时间日期]
2010/03/15 PHP
PHP对象克隆clone用法示例
2016/09/28 PHP
PHP代码重构方法漫谈
2018/04/17 PHP
Flash对联广告的关闭按钮讨论
2007/01/30 Javascript
javascript 数组排序函数sort和reverse使用介绍
2013/11/21 Javascript
浅析JavaScript中的事件机制
2015/06/04 Javascript
JS简单实现城市二级联动选择插件的方法
2015/08/19 Javascript
js实现文本框只允许输入数字并限制数字大小的方法
2015/08/19 Javascript
json+jQuery实现的无限级树形菜单效果代码
2015/08/27 Javascript
jquery实现红色竖向多级向右展开的导航菜单效果
2015/08/31 Javascript
jQuery EasyUI之DataGrid使用实例详解
2016/01/04 Javascript
js实现登录框鼠标拖拽效果
2017/03/09 Javascript
详解vue 数组和对象渲染问题
2018/09/21 Javascript
javascript中toFixed()四舍五入使用方法详解
2018/09/28 Javascript
搭建基于express框架运行环境的方法步骤
2018/11/15 Javascript
微信小程序登录数据解密及状态维持实例详解
2019/05/06 Javascript
Vue使用localStorage存储数据的方法
2019/05/27 Javascript
vue实现路由监听和参数监听
2019/10/29 Javascript
基于Python中capitalize()与title()的区别详解
2017/12/09 Python
对numpy中二进制格式的数据存储与读取方法详解
2018/11/01 Python
Python实现简单石头剪刀布游戏
2021/01/20 Python
Python变量访问权限控制详解
2019/06/29 Python
使用Keras预训练好的模型进行目标类别预测详解
2020/06/27 Python
pyqt5 textEdit、lineEdit操作的示例代码
2020/08/12 Python
Python + opencv对拍照得到的图片进行背景去除的实现方法
2020/11/18 Python
Python getsizeof()和getsize()区分详解
2020/11/20 Python
HTML5 用动画的表现形式装载图像
2016/03/08 HTML / CSS
HTML5实现可缩放时钟代码
2017/08/28 HTML / CSS
餐饮主管岗位职责
2013/12/10 职场文书
优秀党支部事迹材料
2014/01/14 职场文书
国税会议欢迎词
2014/01/16 职场文书
售后服务经理岗位职责范本
2014/02/22 职场文书
预备党员表决心书
2014/03/11 职场文书
产品质量承诺书范文
2014/03/27 职场文书
SQL解决未能删除约束问题drop constraint
2022/05/30 SQL Server