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 值列举的程序为:
以上就是安达网络工作室关于《解析WordPress中的post_class与get_post_class函数》的一些看法。更多内容请查看本栏目更多内容!
get_header()(获取头部) 引入主题的头部模板,默许会引入以后主标题录里的 header.php 文件。假如指定了一...
本文实例讲述了WordPress分类与标签等存档页完成置顶的办法。分享给大家供大家参考。详细剖析如下: 在word...
前几天见到某 Blog (遗记名字和网址了) 有一个相当适用的评论性能. 访客留言之后材料输出框会被暗藏起来, 好...
在帮King改他的公家情侣博客模版~找了一些找了,总算是把工夫显示为想要的格局了。于是将取得的一些信息记下...
默许状况下,WordPress 存档页面或搜寻后果页面中的日志陈列秩序是依照重新到旧来陈列的,这里咱们暂且不论...
咱们常见的WordPress站点文章排序,通常状况下是按公布工夫的升序或降序形式陈列,这种排序形式简直能够满足...