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
1 Answer
Reset to default 3One 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
版权声明:本文标题:How do I add class to an admin comment? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745275843a2651179.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论