[WordPress教程] post_class-WordPress过滤器教程 -52JYU.CN

2021-09-25 0 910

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()

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

静鱼客栈 学习中心 [WordPress教程] post_class-WordPress过滤器教程 -52JYU.CN https://wp.muooy.cn/9257.html

静鱼客栈的帅逼站长~

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务