Divi WordPress Theme
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

WordPress fix Reply To: How to display a list of all the commenters on a single post

Discussion in 'Misc WordPress Requests' started by Sally, Feb 22, 2018.

  1. Sally

    Guest

    Reply To: How to display a list of all the commenters on a single post, by Sally

    Try this:

    <?php
    function get_comment_authors_list( $id = null ) {
    $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 ) {
    $arr = explode( ' ', trim( $comment->comment_author ) );
    if ( ! empty( $arr[0] ) && ! in_array( $arr[0], $names ) ) {
    $names[] = $arr[0];
    }
    }
    unset( $comments );

    return implode( ', ', $names );
    }
    }

    add_shortcode( 'comment_authors_list', 'comment_authors_list_shortcode' );
    function comment_authors_list_shortcode( $atts = array() ) {
    $atts = shortcode_atts( array(
    'post_id' => 0,
    ), $atts );

    return get_comment_authors_list( $atts['post_id'] );
    }

    In the post content, you can use this Shortcode:

    • [comment_authors_list /]
    • [comment_authors_list post_id="1" /] (comments on post ID 1)

    In template files (e.g. single.php), you can use:

    • get_comment_authors_list()
    • get_comment_authors_list( 1 ) (comments on post ID 1)

    Note that only authors of approved comments are included in the list. Pingbacks and trackbacks are also ignored.

    Let me know if you need assistance on installing or using the code.

    Reply To: How to display a list of all the commenters on a single post
     
    #1

Share This Page

Monarch Social Sharing Plugin