admin管理员组

文章数量:1417680

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

本文标签: How do I add class to an admin comment