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 HTMLtag.函数用法
<?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-’字样
- 发布状态
- 文章格式
- 是否需要密码
- 文章所述分类(会逐个罗列所述分类)
- 文章所述标签(会逐个罗列标签)
函数声明
/** * Retrieve the classes for the post div as an array. * * The class names are add are many. If the post is a sticky, then the 'sticky' * class name. The class 'hentry' is always added to each post. For each * category, the class will be added with 'category-' with category slug is * added. The tags are the same way as the categories with 'tag-' before the tag * slug. All classes are passed through the filter, 'post_class' with the list * of classes, followed by $class parameter value, with the post ID as the last * parameter. * * @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. * @return array Array of classes. */function get_post_class( $class = '', $post_id = null ) { $post = get_post($post_id); $classes = array(); if ( empty($post) ) return $classes; $classes[] = 'post-' . $post->ID; $classes[] = $post->post_type; $classes[] = 'type-' . $post->post_type; $classes[] = 'status-' . $post->post_status; // Post Format if ( post_type_supports( $post->post_type, 'post-formats' ) ) { $post_format = get_post_format( $post->ID ); if ( $post_format && !is_wp_error($post_format) ) $classes[] = 'format-' . sanitize_html_class( $post_format ); else $classes[] = 'format-standard'; } // post requires password if ( post_password_required($post->ID) ) $classes[] = 'post-password-required'; // sticky for Sticky Posts if ( is_sticky($post->ID) && is_home() && !is_paged() ) $classes[] = 'sticky'; // hentry for hAtom compliance $classes[] = 'hentry'; // Categories if ( is_object_in_taxonomy( $post->post_type, 'category' ) ) { foreach ( (array) get_the_category($post->ID) as $cat ) { if ( empty($cat->slug ) ) continue; $classes[] = 'category-' . sanitize_html_class($cat->slug, $cat->term_id); } } // Tags if ( is_object_in_taxonomy( $post->post_type, 'post_tag' ) ) { foreach ( (array) get_the_tags($post->ID) as $tag ) { if ( empty($tag->slug ) ) continue; $classes[] = 'tag-' . sanitize_html_class($tag->slug, $tag->term_id); } } if ( !empty($class) ) { if ( !is_array( $class ) ) $class = preg_split('#\s+#', $class); $classes = array_merge($classes, $class); } $classes = array_map('esc_attr', $classes); return apply_filters('post_class', $classes, $class, $post->ID);}