解析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语法(1)
Oct 09 PHP
php下把数组保存为文件格式的实例应用
Feb 08 PHP
一篇有意思的技术文章php介绍篇
Oct 26 PHP
延长phpmyadmin登录时间的方法
Feb 06 PHP
php笔记之:php函数range() round()和list()的使用说明
Apr 26 PHP
分享下页面关键字抓取www.icbase.com站点代码(带asp.net参数的)
Jan 30 PHP
利用PHP将部分内容用星号替换
Apr 21 PHP
WordPress中用于获取文章作者与分类信息的方法整理
Dec 17 PHP
PHP类的声明与实例化及构造方法与析构方法详解
Jan 26 PHP
php opendir()列出目录下所有文件的实例代码
Oct 02 PHP
PHP单态模式简单用法示例
Nov 16 PHP
PHP中localeconv()函数的用法
Mar 26 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导出word格式数据的代码实例
2013/11/25 PHP
php设计模式之观察者模式实例详解【星际争霸游戏案例】
2020/03/30 PHP
一些常用的JS功能函数(2009-06-04更新)
2009/06/04 Javascript
jquery模拟SELECT下拉框取值效果
2013/10/23 Javascript
SuperSlide标签切换、焦点图多种组合插件
2015/03/14 Javascript
javascript数组克隆简单实现方法
2015/12/16 Javascript
JS简单去除数组中重复项的方法
2016/09/13 Javascript
jquery判断类型是不是number类型的实例代码
2016/10/07 Javascript
jQuery实现6位数字密码输入框
2016/12/29 Javascript
jQuery 1.9版本以上的浏览器判断方法代码分享
2017/08/28 jQuery
基于Node.js实现压缩和解压缩的方法
2018/02/13 Javascript
JavaScript求一组数的最小公倍数和最大公约数常用算法详解【面向对象,回归迭代和循环】
2018/05/07 Javascript
解决angularjs WdatePicker ng-model的问题
2018/09/13 Javascript
详解Vue2 添加对scss的支持
2019/01/02 Javascript
VUE简单的定时器实时刷新的实现方法
2019/01/20 Javascript
JS三级联动代码格式实例详解
2019/12/30 Javascript
Vue如何基于vue-i18n实现多国语言兼容
2020/07/17 Javascript
python实现统计代码行数的方法
2015/05/22 Python
Python实现按学生年龄排序的实际问题详解
2017/08/29 Python
python正则表达式re之compile函数解析
2017/10/25 Python
Python RabbitMQ消息队列实现rpc
2018/05/30 Python
python爬虫获取小区经纬度以及结构化地址
2018/12/30 Python
python遍历小写英文字母的方法
2019/01/02 Python
解决使用python print打印函数返回值多一个None的问题
2020/04/09 Python
django queryset相加和筛选教程
2020/05/18 Python
Pycharm生成可执行文件.exe的实现方法
2020/06/02 Python
python 如何将office文件转换为PDF
2020/09/22 Python
Django Admin后台模型列表页面如何添加自定义操作按钮
2020/11/11 Python
Helly Hansen工作服美国官方网上商店:为最恶劣的环境
2019/09/04 全球购物
机电专业体育教师求职信
2013/09/21 职场文书
普通院校学生的自荐信
2013/11/27 职场文书
销售文员岗位职责
2013/11/29 职场文书
大学活动邀请函
2014/01/28 职场文书
创业计划书之暑假培训班
2019/11/09 职场文书
python第三方网页解析器 lxml 扩展库与 xpath 的使用方法
2021/04/06 Python
安装配置mysql及Navicat prenium的详细流程
2021/06/10 MySQL