• Hi all,

    I’m having a problem whereby each time I post a test comment, it is always being saved to the very first post.

    The code below is taken from the ‘single.php’. In the first chunk, I want to retrieve the latest post. Then in the second chunk, I want to show a list of the 5 most recent blogs excluding the most recent (already displaying on the page).

    I have tried resetting the $post variable by assigning it to a temporary variable before the ‘foreach’ loop. I also used ‘wp_reset_postdata’… but the problem still occurs.

    The code for the latest post:

    <?php if (have_posts()) :
       while (have_posts()) : the_post();
       global $more;
       $more = 1;
    ?>

    The code for the 5 most recent posts (excluding that already displayed):

    <?php
       global $post;
       $tmp = $post;
       $args = array( 'numberposts' => 5, 'offset' => 1 );
       $myposts = get_posts( $args );
       foreach( $myposts as $post ) : setup_postdata($post); ?>
          <li><a href="<?php the_permalink(); ?>"><span class="a_recent"><?php the_title(); ?><br /><span class="small"><?php the_time('D j M, 'y'); ?></span></span></a></li>
       <?php endforeach;
       $post = $tmp;
       wp_reset_postdata();
     ?>

    Then I include the comments:

    <?php
       $withcomments = 1;
       comments_template();
    ?>

    I used an echo statement to check the value of the $post and it returns the correct number, so I think that the variable is being reset correctly, but somehow the comments are being sent to the wrong place?

    <?php echo '<p>'.get_the_ID().'</p>'; ?>
    ^Shows the correct post number

    Anyone have any ideas, or advice?

    Many thanks in advance

Viewing 1 replies (of 1 total)
  • Have you tried:

    – deactivating all plugins to see if this resolves the problem. If this works, re-activate the plugins one by one until you find the problematic plugin(s).

    – switching to the Twenty Eleven theme to rule out any theme-specific problems.

    resetting the plugins folder by FTP or PhpMyAdmin. Sometimes, an apparently inactive plugin can still cause problems.

Viewing 1 replies (of 1 total)
  • The topic ‘Comments being saved to wrong post… wp_reset_postdata?’ is closed to new replies.