### [如何显示WordPress某个文章所有评论者名称](https://www.prosaas.cn/article/8983) **Published:** 2020-11-10T07:02:05 **Author:** 5201314 **Excerpt:** 下面由WordPress教程栏目给大家介绍显示WordPress某个文章所有评论者名称的办法,希望对需要的朋友… 下面由**WordPress**教程栏目给大家介绍显示WordPress某个文章所有评论者名称的办法,希望对需要的朋友有所帮助! ![](//zhiqipro.oss-cn-beijing.aliyuncs.com/2023/12/20231212131830814.jpg) 如果想显示某篇文章或当前文章所有评论者名称列表,可以参考一下本文的方法。 使用场景,比如在文章适当位置,显示当前已有:史珍香,秦寿生,焦厚根,朱逸群,夏建仁等发表了热情扬溢的评论,再加一个锚点链接,引导读者跳转到评论表单,也发个热情扬溢的评论。 将代码添加到当前主题函数模板functions.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'\] ); } 使用方法: 一、调用ID为:123文章的所有评论者名称 在模板中使用: 在文章添加短代码: \[comment\_authors\_list post\_id="123" /\] 二、调用当前文章所有评论者名称,与上面类似只是去掉其中的文章ID,适合放在文章正文模板中。 在模板中使用 在文章中添加短代码: \[comment\_authors\_list /\] 以上就是如何显示WordPress某个文章所有评论者名称的详细内容,更多请关注知企PROSAAS其它相关文章! **Categories:** WordPress ---