一篇优质的文章,会使得大量的用户进行评论互动,当评论一多,就会让人看得眼花缭乱(有些还有分页功能)。这个时候,我们可以使用一段代码来显示某篇文章的所有评论者。
将以下代码添加到WordPress当前主题的函数模板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\'] );
}使用方法
1.查看文章ID为567,下面所有的评论者有哪些
就在你的模板中添加以下代码
<?php echo get_comment_authors_list(\'123\'); ?>这样就可以显示出所有参与评论的评论者
文章中添加短代码
[comment_authors_list post_id=\"123\" /]2.调用当前文章所有评论者名称,与上面类似只是去掉其中的文章ID,适合放在文章正文模板中。
就在你的模板中添加以下代码
<?php echo get_comment_authors_list(); ?>在文章中添加短代码:
[comment_authors_list /]本站除原创内容,其余内容均收集自互联网,仅限用于学习和研究目的,本站不对其内容的合法性、可用性承担任何责任。本站禁止以任何形式发布或转载任何违法相关信息,若您发现请立即向站长举报;用户投稿一切资源内容不代表本站立场,并不代表本站赞同其观点和对其真实性负责。
如有版权内容,其版权均归原作者所有,本站虽力求保存原有版权信息,但因众多资源经多次转载,已无法确定其真实来源,故敬请原作者谅解!为尊重作者劳动成果,请购买正版支持作者,谢谢!若您对「HULIKU.COM」所载资源作品版权归属存有异议,请发送邮箱:ihuliku@qq.com 进行处理。
本站资源文件大多存储至云盘,如您发现图片或链接失效,请联系站长或作者,我们将及时进行更新。资源都是经过站长或作者收集测试优化后进行发布分享。如若转载请在贵站文内以超链形式注明狐狸库文章出处,谢谢合作!













![WordPress子比主题美化教程合集[2023/04/17]-狐狸库](https://huliku.com/pic/2023032607024935.png)
![狐狸库自定义子比主题底部footer页脚美化(自适应)[06.26]更新-狐狸库](https://huliku.com/pic/2023062610013835.png)























请登录后查看评论内容