How do I add class to an admin comment?

When I customize the wp_list_comments() output, how do I add a class to the admin comments?Relevant code: <?php wp_li

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745275844a4620034.html

相关推荐

  • How do I add class to an admin comment?

    When I customize the wp_list_comments() output, how do I add a class to the admin comments?Relevant code: <?php wp_li

    5小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信