Support » Fixing WordPress » Duplicate post in my recent posts

  • Resolved ES Neubauer


    I’m trying to display my most recent post as an excerpt and 4 other posts as links. I used the code from Multiple_Loops_in_Action to display 2 lists of posts with the $do_not_duplicate = $post->ID bit and every thing is working just fine, except that the most recent post is showing up in the second list as well. I’m new to php and can’t figure out where I am going wrong.

    Here is my code:

    <?php $my_query = new WP_Query('category_name=inspiration&posts_per_page=1');
      while ($my_query->have_posts()) : $my_query->the_post();
      $do_not_duplicate = $post->ID;?>
          <li><a href="<?php echo get_permalink(); ?>">
     <?php the_title( '<h2>', '</h2>' ); ?></a> <?php the_excerpt(); ?></li>
      <?php endwhile; ?>
        <!-- Do other stuff... -->
    <?php if (have_posts()) : while (have_posts()) : the_post();
      if( $post->ID == $do_not_duplicate ) continue; ?>
    	$args = array( 'numberposts' => '5' );
    	$recent_posts = wp_get_recent_posts();
    	foreach( $recent_posts as $recent ){
    		echo '<li><h2><a href="' . get_permalink($recent["ID"]) . '" title="'.esc_attr($recent["post_title"]).'" >' .   $recent["post_title"].'</a> </h2></li> ';
      <?php endwhile; endif; ?>

    And here is a link to the site (the code is in the sidebar.php):


Viewing 4 replies - 1 through 4 (of 4 total)
  • esmi


    Forum Moderator

    Is this the second Loop? If so, you shouldn’t be using query_posts() but WP_Query()

    It is the second loop, but forgive me if I’m wrong since I am very new at this, but it looks to me like I am using WP_Query and not query_posts. It seems to work in the sense that posts are being listed as I wanted them to, except the do_not_duplicate part.



    Forum Moderator

    Oh – sorry!! My bad! If you use the offset parameter, you should be able to skip that first post.

    <?php $my_query = new WP_Query('category_name=inspiration&posts_per_page=1&offset=1');

    Thanks! That did the trick.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Duplicate post in my recent posts’ is closed to new replies.