I'm trying find a way in my single.php to list all users (display name) that haven't commented on the current post.
I have this snippet that lists all the users who have commented...
/**
* Retrieve a list of comment authors' names.
*
* @param int $id Optional. Post ID.
* @param string $sep Optional. List separator.
* @return string|null Names list.
*/
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->first_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 );
}
}
/**
* Display a list of comment authors' names.
*
* @param int $id Optional. Post ID.
* @param string $sep Optional. List separator.
* @return null
*/
function the_comment_authors_list( $id = 0, $sep = ', ' ) {
echo get_comment_authors_list( $id, $sep );
}
/*
* Available parameters for the Shortcode:
* int post_id Optional. Post ID.
* string list_sep Optional. List separator.
*
* Usage examples:
* [comment_authors_list /]
* [comment_authors_list post_id="1" list_sep=" · " /]
*/
add_shortcode( 'comment_authors_list', 'comment_authors_list_shortcode' );
/**
* Shortcode callback function.
*
* @see get_comment_authors_list()
* @return string|null Names list.
*/
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'] );
}
<?php echo get_comment_authors_list(); ?>
into my single.php to display all the comment authors