WordPress某篇文章显示所有评论者名称或头像

在某篇文章或当前文章的适当位置显示所有评论者名称列表,或在文章列表摘要下方显示评论者头像,可以引导读者也发个热情洋溢的评论。

1. 显示评论者名称

1.1 效果

独元殇, 耳朵的主人, 雅岚, Kevin’s, 小彦, 阿和, Mr.Chou等人对本文发表了17条热情洋溢的评论。

1.2 代码

php

  function get_comment_authors_list( $id = 0, $sep = ', ' ) {
    $post_id = $id ? $id : get_the_ID();
    if ( $post_id ) {
      $comments = get_comments( array(
        'post_id' => $post_id,
        'status'  => 'approve',
        'type'    => 'comment',
      ) );

      $names = array();
      foreach ( $comments as $comment ) {
        $name = $comment->comment_author;
        if ( $comment->user_id ) {
          $user = get_userdata( $comment->user_id );
          $name = $user ? $user->display_name : $name;
        }

        $arr = explode( ' ', trim( $name ) );
        if ( ! empty( $arr[0] ) && ! in_array( $arr[0], $names ) ) {
          $names[] = $arr[0];
        }
      }
      unset( $comments );

      $sep = $sep ? $sep : ', ';
      return implode( $sep, $names );
    }
  }
  add_shortcode( 'comment_authors_list', 'comment_authors_list_shortcode' );

  function comment_authors_list_shortcode( $atts = array() ) {
    $atts = shortcode_atts( array(
      'post_id'  => 0,
      'list_sep' => '',
    ), $atts );

    return get_comment_authors_list( $atts['post_id'], $atts['list_sep'] );
  }

1.3 使用方法

1.3.1 调用ID为 123 文章的所有评论者名称,并在模板中使用:
php

<?php echo get_comment_authors_list('123'); ?>
1.3.2 调用ID为 123 文章的所有评论者名称,并在文章添加短代码:
php

[comment_authors_list post_id="123" /]
1.3.3 调用当前文章所有评论者名称,并放在当前文章页面模板中,如本站评论列表上面。
php

<?php echo get_comment_authors_list(); ?>

当然也可以在当前文章中调用:

php

[comment_authors_list /]

2. 显示头像(加强版)

其实在折腾上面名称时特别想弄一个头像版(替代文字名称)。

2.1 代码

php

<?php 
    $nums=20; //要显示几个评论
    $get_comments_num=30; //获取最大评论数,主要用来排除博主的评论。
    $fc_comments = get_comments('status=approve&type=comment&number='.$get_comments_num.'&post_id='.get_the_ID());
    if ( !empty($fc_comments) ) {
        $my_email='nick@abc.com'; //排除管理员的评论
        $i = 1; 
?>
    <div class="fc_comments">
        <ul>
            <?php
                $commentcount = $fc_comments->comment_count;
                $fc_output='';
                foreach ($fc_comments as $fc_comment) {
                    if ($fc_comment->comment_author_email != $my_email) {
                        $fc_avatar=get_avatar($fc_comment->comment_author_email,60);
                        $fc_output .= '<li><a href="'
                        .get_comment_link( $fc_comment->comment_ID, array('type' => 'all')).'">'
                        .$fc_avatar
                        .'</a><ul><li>'
                        .$fc_avatar
                        .'<p>'
                        .'<strong>'.$fc_comment->comment_author.'</strong>'
                        .$fc_comment->comment_date
                        .'</p><p>'
                        .convert_smilies(strip_tags($fc_comment->comment_content))
                        .'</p></li></ul></li>';
                        if ($i == $nums || $i == $commentcount) break; //评论数量达到退出遍历
                        ++$i;
                    }
                }
                echo $fc_output;
                if ($fc_output) echo '
  • ', comments_popup_link('','','» more...'), '
  • '; ?> </ul> </div> <?php } ?>

    2.2 样式

    以下样式仅供参考。

    css
    .fc_comments ul,.fc_comments ul ul p{margin:0;padding:0;}
    .fc_comments ul li{position:relative;list-style:none;float:left;line-height:18px;margin-right:5px;}
    .fc_comments ul li.fc_more{line-height:16px;padding-top:8px;}
    .fc_comments ul li img.avatar{width:20px;height:20px;margin:0;padding:1px;border:1px solid #ddd;}
    .fc_comments ul li img.avatar:hover{border-color:#999;}
    .fc_comments ul li:hover > ul{display:block;}
    .fc_comments ul ul{display:none;position:absolute;bottom:40px;left:0;}
    .fc_comments ul ul li{width:280px;padding:5px;border:2px solid #999;background-color:#fff;}
    .fc_comments ul ul li img.avatar{float:left;width:32px;height:32px;margin-right:5px;}
    

    上述代码和样式供参考,大家可以根据自己喜好和主题进行修改。Enjoy it ~~

    老王发布于2024-07-03 22:47
    酸甜苦辣咸,五味调和,共存相生,百味纷呈。

    赞助 点赞 1

    END, 威言威语, XI等人对本文发表了6条热情洋溢的评论。
  • 好。
    1. 老王说道:
      回复 END: 在干嘛呢?
  • 最近Wordpress整挺多,哈哈
    1. 老王说道:
      回复 威言威语: 没有整,只是水一篇。。。😂
  • 越来越6了
    1. 老王说道:
      回复 XI: 嘿嘿,向你学习。😊
  • 发表回复

    您的电子邮箱地址不会被公开。 必填项已用 * 标注