Support » Fixing WordPress » Issue: query_posts displaying extra post

  • Resolved ellp


    Hello folks!

    I’m creating one theme for WordPress with multiples Loops inside. The code I’m using is someting like that:

    <?php query_posts('showposts=1'); if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <img src="<?php $values = get_post_custom_values("thumbnail"); echo $values[0]; ?>" width="148" height="120" alt="<?php the_title()?>" />					
     <h5><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h5>
    <?php endwhile; else: ?>
    <?php endif; wp_reset_query(); ?>

    But, the output is aways the result +1 extra post. For exemple: in this code above, display 2 posts, but I want only one.

    Another one:

    <?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('sidebar_page_1') ) : ?>
    		<h3>Latest Reviews</h3>
    		<?php $my_query = new WP_Query('showposts=3');
    		  while ($my_query->have_posts()) : $my_query->the_post();?>
    				    <h5><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h5>
    		  <?php endwhile; ?>
    	<?php endif;?>

    This code above output is 4 posts, but I want only 3.

    I’m using starkers theme as basic framework.

    Question: Its a bug or I’m doing something wrong? Any ideas to solve that?

    Thanks! And sorry about my english :/

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


    Forum Moderator

    Try replacing showposts with posts_per_page. If that doesn’t work, it could be something higher up that’s causing you problems.

    You might also want to look at using get_posts instead of query_posts – especially in that second loop since you’re only using the the_permalink and the_title (both of which should be available via get_posts).

    Hello esmi!

    I have tried to use post_per_page too, but I got the same error. About your second idea I will try that. Thanks.

    Hello again esmi!

    I’ve used your tip to solve this issue, and work like a charm! I used a code like:

    		$lastposts = get_posts('numberposts=5');
    		foreach($lastposts as $post) :
    		<h2><a href="<?php the_permalink(); ?>" id="post-<?php the_ID(); ?>"><?php the_title(); ?></a></h2>
    		<?php endforeach; ?>

    Thank You! 😀

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Issue: query_posts displaying extra post’ is closed to new replies.