解析WordPress中的post_class与get_post_class函数

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 p.
 *
 * @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-'字样
  • 公布形态
  • 文章格局
  • 能否需求明码
  • 文章所述分类(会一一列举所述分类)
  • 文章所述标签(会一一列举标签)

以上就是安达网络工作室关于《解析WordPress中的post_class与get_post_class函数》的一些看法。更多内容请查看本栏目更多内容!

版权声明:本文为 安达网络工作室 转载文章,如有侵权请联系我们及时删除。
相关文章
本人做wordpress评论插件修正评论款式(两步丑化评论内容)

wordpress自带的近期评论小工具不会显示详细的评论内容,而且还会显示治理员的评论,觉得不是很好,只能本人...

WordPress中增加语音搜寻性能的完成办法

本文实例讲述了WordPress中增加语音搜寻性能的完成办法。分享给大家供大家参考。详细剖析如下: WordPress能...

wordpress dynamic_sidebar()函数应用办法

dynamic_sidebar()函数用来支持自定义sidebar侧边栏,能够自定义Widget插件,比方为侧边栏增加最近文章,文...

利用phpmyadmin找回wordpress遗记治理明码

1、确认你的数据库名通常有多个数据库或许一个数据库里装置了多套顺序,所以预备工作就是确认你的数据库名和...

调试WordPress中定时义务的相干PHP脚本示例

普通人们应用WordPress的Cron API来完成定时义务,它的原理就是将创立的定时义务存储到数据库里,当有人拜访...

wordpress增加顶级菜单完成思绪

比方本人创立了一个主题,那么需求在后盾增加一些设置选项,所以就触及到了后盾增加顶级菜单的需要: 复制代...

需求提交

客服服务

亿鸽在线客服系统