• Hello,

    I’m looking for a piece of PHP code or something similar that would list the names of those who have commented on a post, at the bottom of each post.

    I am thinking of something like this:

    “blah blah blah end of blog here.

    People who have commented: Name, Name, Name, Name.”

    Is there a way to ‘plug’ the commenters names this way?
    Thanks for help in advance!

Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    If you use get_comments, passing it the current post ID, it will return an array of comment objects, which you can iterate to get the author names.

    https://codex.wordpress.org/Function_Reference/get_comments

    Check the first code example on that page.

    Thread Starter annabellegralton

    (@annabellegralton)

    Hi! Thanks for your response.

    I seem to be on the correct-ish trail. I’m a beginner at PHP, so please bare with me.
    I added <?php the_ID(); ?> to the PHP code below, replacing the post_id=15 in the original on the page. I added this below the <?php the_content(__('(more...)')); ?> on the index.php page of my theme.

    
    <?php 
    $comments = get_comments('<?php the_ID(); ?>');
    foreach($comments as $comment) :
    	echo($comment->comment_author);
    endforeach;
    >
    

    It works, in that it gives me the names of the commenters, however it lists every commenters name on the bottom all posts, including ones that have no comments or comments from different people.
    I’m not sure how to change this, or make the names themselves link to the authors url.

    I hope that makes sense!

    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    That code doesn’t make sense

    $comments = get_comments(‘<?php the_ID(); ?>’);

    use

    $id = get_the_ID();
    $comments = get_comments($id);
    Thread Starter annabellegralton

    (@annabellegralton)

    Hi, thanks for the feedback.
    I changed the code, it is still showing the original result on all blog posts.
    I’m researching it all now behind the scenes.

    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    This goes in your theme’s functions.php

    function show_commenters( $content ) {
            if ( ! is_single() ) {
                    return $content;
            }
            $commenters = array();
            $id         = 'post_id=' . get_the_ID();
            $comments   = get_comments( $id );
            foreach ( $comments as $comment ) {
                    $commenters[] = $comment->comment_author;
            }
            if ( sizeof( $commenters ) == 0 ) {
                    return $content;
            }
    
            return   $content . '<div class="commenters">Comments by ' . implode( ', ', $commenters ) . '</div>';
    }
    add_filter( 'the_content', 'show_commenters' );
    Thread Starter annabellegralton

    (@annabellegralton)

    Hi,

    Just added that to functions.php, no difference so far – would it help if I messaged / sent you the theme or a link to the site?

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Show the Comment Authors’ names at the bottom of posts.’ is closed to new replies.