apply_filters( 'post_class', array $classes, array $class, int $post_id )
描述
过滤当前帖子的CSS类列表。
参数
$class
(array) 一个post类的数组。
$class
(array) 添加到帖子中的其他类的数组。
$ POST_ID
(int) 帖子ID。
例子
调用函数时调用过滤器get_post_class($class, $post_id)。它在功能结束时被调用。
内部WordPress会在许多地方调用此功能,包括管理仪表板页面。该过滤器允许您过滤$classes将添加到页面的Array 。
此外,通过说插件或模板传递给函数调用的类将作为第二个参数传递$class,最后第三个参数是由函数处理的后置对象的ID。
add_filter('post_class', 'set_row_post_class', 10,3);
function set_row_post_class($classes, $class, $post_id){
if (!is_admin()) { //make sure we are in the dashboard
return $classes;
}
$screen = get_current_screen(); //verify which page we're on
if ('my-custom-type' != $screen->post_type && 'edit' != $screen->base) {
return $classes;
}
//check if some meta field is set
$profile_incomplete = get_post_meta($post_id, 'profile_incomplete', true);
if ('yes' == $profile_incomplete) {
$classes[] = 'profile_incomplete'; //add a custom class to highlight this row in the table
}
// Return the array
return $classes;
}
源文件
wp-includes / post-template.php: get_post_class()