0

I am working on a self-made theme and I have an issue where all of the comments are showing up on all the posts, instead of each post only showing their own comments.

I am using this code to display comments:

<?php if (have_posts()): ?>
  <?php while (have_posts()): ?>
    <?php the_post(); ?>
    // The title, content and some other html here
    wp_list_comments(array( 'callback' => 'comments_callback'), get_comments());
  <?php endwhile; ?>
<?php endif; ?>

And I have tried to both put this line inside and outside the loop, and still get the same result. I got com comments_callback funktion in a mu-plugins and the code for it looks like this:

<?php
function comments_callback( $comment, $args, $depth ) {
    $GLOBALS['comment'] = $comment;
    ?>
    <div class="the-comment">
      <div class="blog-comment-avatar">
        <i class="fa fa-user" aria-hidden="true"></i>
      </div>
      <div class="comment-text">
      <p class="text-secondary comment-author"><?php comment_author(); ?></p>
        <?php comment_text(); ?>
      </div>
    </div>
    <?php
}
?>

What am I doing wrong here?

  • Please add the full code of your template? – Ahmed Fouad Sep 26 '16 at 0:00
  • This can happen when you are overwriting the main query with a second, broken one. – fuxia Sep 26 '16 at 0:18
  • I dont know what else you want? The whole mu-pugin is listed above, and the post is a standard loop containing the first-line. Nothing special or advanced. – Alex Sep 26 '16 at 0:46
  • @AhmedMahdi I have now updated my question with some more code showing how the loop looks like and where the comments are outputed – Alex Sep 26 '16 at 3:14
  • haven't tested your code but it looks like comment_author and comment_text need a $comment_ID that is not being passed correctly developer.wordpress.org/reference/functions/comment_text Have you tried querying comments directly? codex.wordpress.org/Class_Reference/WP_Comment_Query – The J Sep 26 '16 at 3:22
1

I was able to reproduce the issue. It's being caused by the loop in your code:

<?php if (have_posts()): ?>
  <?php while (have_posts()): ?>

Replace that standard loop with if ( have_comments() ) {..., like this:

<?php if ( have_comments() ) { ?>
    // The title, content and some other html here
    wp_list_comments(array( 'callback' => 'comments_callback'), get_comments());
<?php endif; ?>
  • Glad to hear! That was a sneaky one. – Dave Romsey Sep 26 '16 at 3:51
  • 1
    Yes, it sure was! I've been really confused over this one since it was'nt a custom loop or anything, thanks alot for the help! :) – Alex Sep 26 '16 at 3:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.