• Resolved gianko_olivieri

    (@gianko_olivieri)


    so.. i modified my single.php file to show the latest five post in that category this way:

    <?php  $my_query = new WP_Query('cat=' . $numero. '&showposts=5');
      while ($my_query->have_posts()) : $my_query->the_post();
      $do_not_duplicate = $post->ID;?>
        <a href="<?php the_permalink() ?>" rel="bookmark" title="Link Permanente a <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
    
      <?php endwhile; ?>

    that code is after the loop and before the comments.

    And now the comments in one post appear in other posts.. haha…

    the problem, i guest, is in this line:
    $my_query = new WP_Query(‘cat=’ . $numero. ‘&showposts=5’);

    Help, please ??

Viewing 4 replies - 1 through 4 (of 4 total)
  • When you’re done your “latest five posts” loop, try

    <?php if (have_posts()) : while (have_posts()) : the_post();
          update_post_caches($posts); ?>
       <!-- Do comment stuff here... -->
      <?php endwhile; endif; ?>

    This is all documented on https://codex.www.ads-software.com/The_Loop#Multiple_Loops but you need to carefully read through all the examples to understand it, unfortunately.

    Thread Starter gianko_olivieri

    (@gianko_olivieri)

    Yeah!!! thanks, that fixed it!…

    here is the final code, if someone is having the same problem.

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    	<!-- POST STUFF -->
    	<?php endwhile; else: ?>
    	<!-- 404 STUFF -->
    <?php endif; ?>
    <?php  $my_query = new WP_Query('cat=X&showposts=5');
    	while ($my_query->have_posts()) : $my_query->the_post();
    	$do_not_duplicate = $post->ID;?>
    	<!-- 5 LATEST POST STUFF -->
    	<?php endwhile; ?>
    <?php if (have_posts()) : while (have_posts()) : the_post();
    	update_post_caches($posts); ?>
    	<!-- COMMENTS STUFF -->
    <?php endwhile; endif; ?>

    where X is the Cat ID

    I don’t have

    <?php if (have_posts()) : while (have_posts()) : the_post();

    on comments.php

    i only have

    <?php comments_template(); ?>

    @marcomail, we’re talking about a posts query. comments.php is only supposed to display comments, not posts. Look in single.php. That page typically includes comments.php, so on a single post page, it uses the $wp_query->comments list to display the comments for the related post.

    If you’re writing a custom query (like the posters above), then we’re explaining that you need to restore the default $wp_query global variable, otherwise your comments_template() function call will use the most recent query data, and display the wrong comments or none at all.

    I hope that’s why you asked your question.

    BTW there is a have_comments() loop, but for comments.php customization you should stick with the default comments.php and just use CSS, or make a custom Walker class if you like to dig into PHP.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Weird comments behavior after using WP_query()’ is closed to new replies.