在文章适当位置显示已有评论者的评论,引导读者跳转到评论表里发表评论,那么如何只显示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 /]

相关新闻

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

联系我们

联系我们

151-1101-0018

在线咨询:
邮件:30115776@qq.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信
关注微信
分享本页
返回顶部