温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

显示WordPress某个文章所有评论者名称的实现方法

发布时间:2020-12-11 14:23:43 阅读:289 作者:小新 栏目:建站服务器
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

小编给大家分享一下显示WordPress某个文章所有评论者名称的实现方法,希望大家阅读完这篇文章后大所收获,下面让我们一起去探讨吧!

使用场景,比如在文章适当位置,显示当前已有:史珍香,秦寿生,焦厚根,朱逸群,夏建仁等发表了热情扬溢的评论,再加一个锚点链接,引导读者跳转到评论表单,也发个热情扬溢的评论。

将代码添加到当前主题函数模板functions.php中:

function get_comment_authors_list( $id 0$sep ', ' {
$post_id $id $id get_the_ID();
if $post_id ) {
$comments get_commentsarray(
'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_attsarray(
'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 /]

看完了这篇文章,相信你对显示WordPress某个文章所有评论者名称的实现方法有了一定的了解,想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI

开发者交流群×