show comments of a post below the post in single.php wordpress

I have single.php file and Im showing single post in it. I want to show comments on related to this post under this post. But the problem is Im getting comments all of comments below any post.

Below is my single.php code

Read More
    <?php
/*
* The template for displaying all single posts and attachments
*/
?>
<?php get_header(); ?>
<div id="single_post_wrap">
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <h2><?php the_title(); ?></h2>
    <div class="time_and_author"><?php the_time('F jS, Y') ?> by <?php the_author() ?></div>
    <div class="post_content"><?php the_content(); ?></div>
    <p>Posted in <?php the_category(', ') ?> | <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?></p>
    <?php endwhile; endif; ?>
        <?php  foreach (get_comments() as $comment): ?>
        <div><?php echo $comment->comment_author; ?> said: "<?php echo $comment->comment_content; ?>".</div>
        <?php endforeach; ?>
    <?php comments_template(); ?>    
</div>
</div>
<?php get_footer(); ?>

Below is my comments.php code

<?php $comment_args = array(
        'comment_notes_after' => '',
        'title_reply' => 'Have something to say?'
    ) ?>

Thanks in advance.

Related posts

Leave a Reply

3 comments

  1. use is_single() to check the post :

    <?php if( is_single() ) : ?>
    
    <?php  foreach (get_comments() as $comment): ?>
        <div><?php echo $comment->comment_author; ?> said: "<?php echo $comment->comment_content; ?>".</div>
        <?php endforeach; ?>
    <?php comments_template(); ?>      
    
    <?php endif; // close to check single.php ?>
    
  2. Remove the foreach from single.php:

    <?php get_header(); ?>
    <div id="single_post_wrap">
        <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
        <h2><?php the_title(); ?></h2>
        <div class="time_and_author"><?php the_time('F jS, Y') ?> by <?php the_author() ?></div>
        <div class="post_content"><?php the_content(); ?></div>
        <p>Posted in <?php the_category(', ') ?> | <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?></p>
        <?php comments_template(); ?>
        <?php endwhile; endif; ?>    
    </div>
    </div>
    <?php get_footer(); ?>
    

    Then, add it to your comments.php and pass $comment_args in get_comments() like so:

    <?php $comment_args = array(
            'comment_notes_after' => '',
            'title_reply' => 'Have something to say?'
          )
    ?> 
    <?php foreach (get_comments($comment_args) as $comment): ?>
        <div>
            <?php echo $comment->comment_author; ?> said: "<?php echo $comment->comment_content; ?>".
        </div>
    <?php endforeach; ?>
    

    have a look the the function reference for get_comments to see the list of arguments you can pass into get_comments

  3. Single.php code. Remove get_comments()

    <?php
    /*
    * The template for displaying all single posts and attachments
    */
    ?>
    <?php get_header(); ?>
    <div id="single_post_wrap">
        <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
        <h2><?php the_title(); ?></h2>
        <div class="time_and_author"><?php the_time('F jS, Y') ?> by <?php the_author() ?></div>
        <div class="post_content"><?php the_content(); ?></div>
        <p>Posted in <?php the_category(', ') ?> | <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?></p>
        <?php endwhile; endif; ?>
            <?php  foreach ($comments as $comment): ?>
            <div><?php echo $comment->comment_author; ?> said: "<?php echo $comment->comment_content; ?>".</div>
            <?php endforeach; ?>
        <?php comments_template(); ?>    
    </div>
    </div>
    <?php get_footer(); ?>
    

    You need to get_the_ID() per single page comment, then add the comment query.

    $id - get_the_ID();
    <?php $comment_args = array(
            'comment_notes_after' => '',
            'title_reply' => 'Have something to say?',
            'post__in' => $id, //Retrieves comments for an array of posts
            'post_id' => $post_ID // Post's ID you can make sure only comments related to that post appear.
          )
    ?> 
    // The comment query
       $comments_query = new WP_Comment_Query;
       $comments = $comments_query->query( $args );