解析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的SEO优化相干的一些PHP页面脚本技巧

随着搜寻引擎大兴, 陈列在前的网站引入大量流量. 无论是搜寻页面的广告还是查进去的后果, 与搜寻者的指标婚...

wp-Syntax wordpress高亮插件应用办法

首先简略理解下 wp-Syntax 插件,wp-Syntax 是一个针对wordpress的代码高亮插件,最大的优点是简略易用,兼...

在wordpress文章末尾增加内容的简略办法

1、修正文章页面模板single.php关上模版文件中的single.php,在其中搜寻在这行上面加上:复制代码代码如下:...

Ubuntu效劳器上的最优WordPress计划

WordPress是一个经典而又弱小的博客顺序,并且易于装置与搭建,在OpenShift上不花半分钟就能够收费建起一个...

WordPress在线装置主题、插件及疾速公布上传图片时提醒谬误

WordPress在线装置主题、插件,或在应用疾速公布的文章中含有图片时,会提醒: 正在装置主题:Frontier 1.0...

需求提交

客服服务

亿鸽在线客服系统