wordpress教程

WordPress显示文章评论用户名称

我的站长站 2020-10-02 人阅读

如果想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 /]


相关推荐
  • 网站评论
  • 织梦cms畅言评论插件
    织梦cms畅言评论插件

    织梦cms畅言评论插件功能说明:畅言是一个简单而强大的社会化评论及聚合平台。用户可以直接用自己的社会化网络账户在第三方网站发表评论,并且一键评论同步至社交网络将网站内容和自己的评论分享给...

    织梦cms插件 2262 6年前
  • 帝国CMS评论插件7.0&7.2
    帝国CMS评论插件7.0&7.2

    帝国CMS评论插件常见问题:1、页面报错Parse error: syntax error, unexpected &#39;[&#39; in eextendcommentconfig.php on line 98答:PHP版本如果低于5.4,请将PHP文件中涉及到[] 的数组短标签...

    帝国cms插件 2080 6年前
  • 织梦CMS仿畅言评论插件
    织梦CMS仿畅言评论插件

    插件截图插件安装说明1、登录织梦后台,找到左侧菜单‘模块’- ‘上传新模块’2、点击选择文件上传gbk.xml或者utf8.xml,点击确定完成插件的上传3、点击左侧菜单‘模块’- ‘模块管理’,点击插件名称...

    织梦cms插件 2619 5年前
最新更新