最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How do I add class to an admin comment?

programmeradmin0浏览0评论

When I customize the wp_list_comments() output, how do I add a class to the admin comments?

Relevant code:

<?php wp_list_comments( array( 'style' => 'ol', 'callback' => 'custom_list_comments' ) ); ?>

<?php
if( ! function_exists( 'custom_list_comments' ) ):
function custom_list_comments( $comment, $args, $depth ) {
?>
<li id="comment-<?php comment_ID() ?>">
    <?php echo get_avatar( $comment, 48 ); ?>
    <?php comment_text(); ?>
    <span><?php echo get_comment_author() ?></span>
    <time><?php comment_time(); ?></time>
    <?php comment_reply_link( array_merge( $args, array( 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
    <?php edit_comment_link(); ?>
<?php
}
endif;

When I customize the wp_list_comments() output, how do I add a class to the admin comments?

Relevant code:

<?php wp_list_comments( array( 'style' => 'ol', 'callback' => 'custom_list_comments' ) ); ?>

<?php
if( ! function_exists( 'custom_list_comments' ) ):
function custom_list_comments( $comment, $args, $depth ) {
?>
<li id="comment-<?php comment_ID() ?>">
    <?php echo get_avatar( $comment, 48 ); ?>
    <?php comment_text(); ?>
    <span><?php echo get_comment_author() ?></span>
    <time><?php comment_time(); ?></time>
    <?php comment_reply_link( array_merge( $args, array( 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
    <?php edit_comment_link(); ?>
<?php
}
endif;
Share Improve this question asked Aug 2, 2019 at 8:23 MatthewMatthew 1515 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 3

One way to add some custom class to a li tag if the comment was posted by an administrator is to replace line

<li id="comment-<?php comment_ID() ?>">

with

<li <?php comment_class(); ?> id="comment-<?php comment_ID() ?>">

This will add support for the default WP comments classes to your comments callback.

Once you do that add in your theme functions.php file the code below it will add class "posted-by-admin" to the li tag classes if the comment author is an administrator.

add_filter( "comment_class", function( $classes, $class, $comment_id, $comment ) {
    if( $comment->user_id > 0 && $user = get_userdata( $comment->user_id ) && user_can( $comment->user_id, "administrator" ) ) {
        $classes[] = "posted-by-admin";
    }
    return $classes; 
}, 10, 4 );
发布评论

评论列表(0)

  1. 暂无评论