Home > CMS Tutorial > WordPress > How to display the names of all commenters on a certain post in WordPress

How to display the names of all commenters on a certain post in WordPress

藏色散人
Release: 2020-11-10 15:02:05
forward
2613 people have browsed it

The following tutorial column of WordPress will introduce to you how to display the names of all commenters on an article in WordPress. I hope it will be helpful to friends in need!

How to display the names of all commenters on a certain post in WordPress

If you want to display a list of the names of all commenters on a certain article or the current article, you can refer to the method in this article.

Usage scenario, for example, in the appropriate position of the article, it is displayed that currently: Shi Zhenxiang, Qin Shousheng, Jiao Hougen, Zhu Yiqun, Xia Jianren, etc. have published enthusiastic comments, and an anchor link is added to guide readers to jump Go to the comment form and leave a passionate comment too.

Add the code to the current theme function template 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'] );
}
Copy after login

Usage method:

1. The calling ID is: 123 All commenter names of the article

Use in the template:

<?php echo get_comment_authors_list(&#39;123&#39;); ?>
Copy after login

Add short code in the article:

[comment_authors_list post_id="123" /]
Copy after login

2. Call the names of all commentators of the current article, similar to the above except remove the article ID, suitable Place it in the article body template.

Use

<?php echo get_comment_authors_list(); ?>
Copy after login
in template

Add shortcode to post:

[comment_authors_list /]
Copy after login

The above is the detailed content of How to display the names of all commenters on a certain post in WordPress. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:zmingcx.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template