解析WordPress中的post_class与get_post_class函数

 更新时间:2016年01月04日 17:19:18   作者:稍息少年  
这篇文章主要介绍了WordPress中的post_class与get_post_class函数,包括post_class()的PHP源码的相应介绍,需要的朋友可以参考下

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实现敏感关键词过滤的示例代码

    这篇文章主要为大家详细介绍了如何通过php实现关键词过滤,文中的示例代码讲解详细,具有一定的参考价值,有需要的小伙伴可以参考下
    2023-11-11
  • 非常实用的php弹出错误警告函数扩展性强

    非常实用的php弹出错误警告函数扩展性强

    利用php弹出警告的函数,整理到自己的代码库中去在遇到错误的地方可用到此函数,扩展性强,喜欢的朋友可以收藏下
    2014-01-01
  • 采用header定义为文件然后readfile下载(隐藏下载地址)

    采用header定义为文件然后readfile下载(隐藏下载地址)

    有时候我们为了隐藏真实的下载地址,我们通过采用header定义为文件然后readfile下载,但这样会加大服务器的负担,一般不建议下载量比较大的文件
    2014-01-01
  • PHP小技巧搜集,每个PHPer都来露一手

    PHP小技巧搜集,每个PHPer都来露一手

    PHP小技巧搜集,每个PHPer都来露一手...
    2007-01-01
  • php at(@)符号的用法简介

    php at(@)符号的用法简介

    对于php的初学者,经常会看到在一个函数名字前面有一个at(@)符号,但是不知道它的作用和用法,去掉后好像对程序没有什么影响,感到很困惑,简单的说@可以忽略错误,有仰制错误的功能为错误控制操作符.
    2009-07-07
  • PHP使用mysql_fetch_object从查询结果中获取对象集的方法

    PHP使用mysql_fetch_object从查询结果中获取对象集的方法

    这篇文章主要介绍了PHP使用mysql_fetch_object从查询结果中获取对象集的方法,实例分析了php操作mysql_fetch_object查询数据库的技巧,非常具有实用价值,需要的朋友可以参考下
    2015-03-03
  • discuz的php防止sql注入函数

    discuz的php防止sql注入函数

    最早开始学习php的时候根本没考虑过安全方面的问题,那时候就是想能做出功能就是万岁了。随着做项目的时间慢慢加长,越来越感觉到网站安全方面的问题十分重要。
    2011-01-01
  • Session保存到数据库的php类分享

    Session保存到数据库的php类分享

    Session保存到数据库的php类,需要的朋友可以参考下。
    2011-10-10
  • php获取客户端IP及URL的方法示例

    php获取客户端IP及URL的方法示例

    这篇文章主要介绍了php获取客户端IP及URL的方法,涉及php预定义服务器变量$_SERVER相关使用技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2017-02-02
  • php解决DOM乱码的方法示例代码

    php解决DOM乱码的方法示例代码

    最近在工作的时候遇到一个问题,在使用DOM的时候,发现了乱码的问题,后来通过查找网上的资料终于解决了,现在将解决的方法分享给大家,感兴趣的朋友们可以参考借鉴,有需要的朋友们下面来一起学习学习吧。
    2016-11-11

最新评论