TAGS :Viewed: 7 - Published at: a few seconds ago

[ Display all recent comments on single post wordpress ]

So far i have added a sidebar which is used in blog as well as in single blog post. I have added a recent comment section in the sidebar which is displayed accordingly in the blog page but the problem is that it wont display in the single post. Here is the code that shows the most recent comments. This work in blog page but shows empty in single post. How can i display the recent comments in single page ?

$comments = get_comments( 'number=4' );
 foreach( $comments as $comment ){
 $comment_title = get_the_title( $comment->comment_post_ID );
 $comment_link = get_comment_link( $comment->comment_ID );
 $comment_temp = get_comment( $comment->comment_ID, ARRAY_A );
 $comm_content = $comment_temp['comment_content'];
 $post_id = $comment->comment_post_ID;
 $images_id = get_post_thumbnail_id( $post_id );
 $images_url = wp_get_attachment_image_src( $images_id, 'recent-blog', true );
                      <li class="list-group-item">
                        <div class="row">
                            <div class="col-sm-4">
                                    <a href="<?php echo($comment_link)?>"><img src="<?php echo $images_url[0];?>" alt="latest" class="img-responsive"></a>

                            <div class="col-sm-8">
                                <h4><a href="<?php echo($comment_link)?>"><?php echo( $comment->comment_author );?></a></h4>
                                <p><?php echo comment_excerpt();?></p>
                      <?php } ?>


Answer 1

You should take a look at the wp_list_comments() function, specially at the "Display Comments for a Specific Page/Post" section...