有一个过滤器叫做comment_class
为此:
apply_filters( \'comment_class\', $classes, $class, $comment->comment_ID, $comment, $post_id );
Source
只需使用参数
comment
这是一个对象
$comment->user_id
这对于获取用户角色和添加类很有用。
编辑:
add_filter( \'comment_class\', \'wpse_253517_comment_class\', 10, 5 );
function wpse_253517_comment_class( $classes, $class, $comment_ID, $comment, $post_id ) {
if ( 0 === (int) $comment->user_id ) {
return $classes;
}
$user_data = get_userdata( $comment->user_id );
$role = reset( $user_data->roles );
switch( $role ) {
case \'subscriber\':
$classes[] = \'is-subscriber\';
break;
// here would be other cases, other roles
default:
return $classes;
}
return $classes;
}