[resolved] Issue: query_posts displaying extra post (4 posts)

  1. ellp
    Posted 6 years ago #

    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 :/

  2. esmi
    Forum Moderator
    Posted 6 years ago #

    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).

  3. ellp
    Posted 6 years ago #

    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.

  4. ellp
    Posted 6 years ago #

    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! :D

Topic Closed

This topic has been closed to new replies.

About this Topic