解析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分类,标签,分页显示及翻页呈现404谬误的处理办法

本文实例讲述了Wordpress分类,标签,分页显示及翻页呈现404谬误的处理办法。分享给大家供大家参考。详细剖析...

WordPress应用中文用户名的办法

在WordPress上增加中文用户时报错说不支持中文,失去如下谬误:谬误:请填写用户名。谬误:此用户名蕴含有效...

WordPress应用RSS Feed输入自定义文章类型内容的办法

本文实例讲述了WordPress应用RSS Feed输入自定义文章类型内容的办法。分享给大家供大家参考。详细剖析如下:...

WordPress主题构造是如何架构的图文分析

利用弱小的技术,能够把基于WordPress的网站做成各种各样的方式,这除了要求WordPress主题开发人员通晓HTML...

简介WordPress中用于获取首页和站点链接的PHP函数

home_url()(获取首页链接) ome_url() 函数用来获取 WordPress 的首页链接。 用法 home_url( $path, $sch...

wordpress增加mp3音频文件教程

上传音频和视频文件应用媒体上传到WordPress是很容易的。虽然媒体上传意识到这些文件,并显示相应的文件类型...

需求提交

客服服务