Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've created sidebar tabs that display the most recent, popular and random posts. In other words I'm creating several loops in my sidebar. This seems to cause some problems, though. Do I have to reset something or just use a complete different code overall?

The code for the sidebar tabs looks like this:

           <div id="tabvanilla" class="widget">

    <ul class="tabnav">
    <li><a href="#popular"><img src="http://zoomingjapan.com/wp-content/themes/alltuts/images/41.gif" border="0" alt="41" /> Popular</a></li>
    <li><a href="#recent">Recent</a></li>
    <li><a href="#random">Random</a></li>
    </ul>

    <div id="popular" class="tabdiv">
<ul id="popular-comments">

<?php
$pc = new WP_Query('orderby=comment_count&posts_per_page=5'); ?>

<?php while ($pc->have_posts()) : $pc->the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_post_thumbnail(array(50,50)); ?></a>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>  <?php comments_popup_link('(0)', '(1)', '(%)'); ?>
<p>Posted on <strong><?php the_time('F jS, Y') ?></strong><br />
<span class="sidebar_content"><?php echo excerpt(8); ?></span></p>
</li>

<?php endwhile; ?>
<?php wp_reset_query(); ?> 
</ul>
    </div><!--/popular-->

    <div id="recent" class="tabdiv">
<ul id="recent_posts">
<?php
$rp = new WP_Query('orderby=date&posts_per_page=5'); ?>

<?php while ($rp->have_posts()) : $rp->the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_post_thumbnail(array(50,50)); ?></a>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>  <?php comments_popup_link('(0)', '(1)', '(%)'); ?>
<p>Posted on <strong><?php the_time('F jS, Y') ?></strong><br />
<span class="sidebar_content"><?php echo excerpt(8); ?></span></p>
</li>

<?php endwhile; ?>
<?php wp_reset_query(); ?> 
</ul>
    </div><!--/recent-->

    <div id="random" class="tabdiv">
<ul id="random_posts">
<?php
$ranp = new WP_Query('orderby=rand&posts_per_page=5'); ?>

<?php while ($ranp->have_posts()) : $ranp->the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_post_thumbnail(array(50,50)); ?></a>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>  <?php comments_popup_link('(0)', '(1)', '(%)'); ?>
<p>Posted on <strong><?php the_time('F jS, Y') ?></strong><br />
<span class="sidebar_content"><?php echo excerpt(8); ?></span></p>
</li>

<?php endwhile; ?>
<?php wp_reset_query(); ?> 
</ul>
    </div><!--random-->

    </div><!--/widget-->

The current issue is that the first tab "Popular" displays 5 posts as it should, but the other 2 tabs display 6 posts, although I've clearly coded it so that only 5 should be displayed!!!!

My website is here for reference (if you use IE - and I hope you don't - please don't be shocked that apart from the tab above none other is working - there are a lot of issues in IE suddenly (since yesterday) possibly caused by CSS, maybe some HTML error - am working on it currently).

Thanks a lot in advance!

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

Have you tried calling <?php wp_reset_postdata(); ?> after the ehdwhile; for each custom loop?

EDIT

Next step is to try calling <?php wp_reset_query(); ?> instead of <?php wp_reset_postdata(); ?> after the ehdwhile; for each custom loop.

Note: see here for a nice explanation of the different ways to reset the WordPress loop.

Note 2: problem is caused by sticky post with title 'under construction'

share|improve this answer
    
I modified my code in my question above according to your suggestion. However, the exact same error still occurs. –  japanworm Sep 17 '11 at 12:48
1  
See answer edit. –  Chip Bennett Sep 17 '11 at 13:04
    
Thank you, but that didn't do the trick either (see edit above). Could something else possibly be interfering or is that not very likely as the first tab works perfectly fine? –  japanworm Sep 17 '11 at 13:15
1  
it might have to do with the sticky post 'under construction'. codex.wordpress.org/Class_Reference/… –  Michael Sep 17 '11 at 13:31
    
Awesome, that did the trick!! You want to post this as answer? :) Thank you so much! –  japanworm Sep 17 '11 at 17:07
show 3 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.