如何只显示WordPress某文章的所有评论者名称?

在文章适当位置显示已有评论者的评论,引导读者跳转到评论表里发表评论,那么如何只显示WordPress某文章的所有评论者名称?今天我们一起学习一下。

如何只显示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’] );

}

使用方法:

一、调用ID为:123文章的所有评论者名称

在模板中使用:

<?php echo get_comment_authors_list(‘123’); ?>

在文章添加短代码:

[comment_authors_list post_id=”123″ /]

二、调用当前文章所有评论者名称,与上面类似只是去掉其中的文章ID,适合放在文章正文模板中。

在模板中使用

<?php echo get_comment_authors_list(); ?>

在文章中添加短代码:

[comment_authors_list /]

小车博客下载必看

给TA打赏
共{{data.count}}人
人已打赏
WP教程

常用的判断标签的WordPress函数

2020-8-27 10:25:04

WP教程

如何实现不登录WordPress后台直接新建管理员账号?

2020-8-28 10:17:29




重要声明

本站资源来自会员发布以及互联网收集,不代表本站立场,如有侵犯你的权益请联系管理员-车车站内发信联系 我们会第一时间进行审核删除。站内资源为网友个人学习或测试研究使用,未经原版权作者许可,禁止用于任何商业途径!请用户仔细辨认内容的真实性,避免上当受骗!本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。请在下载24小时内删除!


如果遇到付费才可观看的文章,建议升级终身VIP。全站所有资源任意下免费看”。本站资源少部分采用7z压缩,为防止有人压缩软件不支持7z格式,7z解压,建议下载7-zip,zip、rar解压,建议下载WinRAR

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索