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'm trying to add a widget area in between posts on the home page. I got them to show right where I want them, after the first post. The problem is that the first two posts are duplicated. The same post that shows above the new widget area is also shown after the widget area. If I take out the call for widget code and just place regular ad code then the posts don't duplicate. This only happens when I call for the widget. I'm not sure how else I could fix this.

Here's what my code looks like in index.php

<?php $postnum = 1; $showads = 1; $counter = 1; ?>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

        <article class="article">
        <div class="post-single post_box">
            <header>
            <div class="headline_area">
            <h2 class="entry-title"><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
        <?php if($options['mts_headline_meta'] == '1') { ?>
            <div class="headline_meta">
            <p class="theauthor"><?php _e('By ', 'mythemeshop'); the_author_posts_link(); ?></p>
            <p class="themeta"><span class="thetime"><?php the_time('F j, Y'); ?></span><span class="thecategories"><?php the_category(' ') ?></span><span class="thecomments"><a href="<?php comments_link(); ?>" rel="nofollow"><?php comments_number('No comments','1 Comment','% Comments'); ?></a></span></p>
            </div>
        <?php } ?>
            </div><!--.headline_area-->
            </header>
            <?php if($options['mts_thumbnails'] == '1') { ?>
            <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>" rel="nofollow"><?php if ( has_post_thumbnail() ) { echo '<div class="featured-thumbnail">'; the_post_thumbnail('thumbnail', array('title' => '')); echo '</div>'; } ?></a>
            <?php } ?>
            <div class="format_text entry-content">
                <?php the_excerpt(); ?>
                <p><a href="<?php the_permalink() ?>" class="readmore" rel="nofollow">Read More...</a></p>
            </div>
        </div><!--.post-single-->
        </article>

        <?php if ($postnum == $showads) { ?>
<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar("Hot-Widget") ) : ?><?php endif; ?>
<?php } $postnum++; ?>

    <?php endwhile; else: ?>

If I add a normal ad code in between the two lines below, then the posts aren't duplicated but whenever I call for the widget then the posts are duplicated.

<?php if ($postnum == $showads) { ?>
<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar("Hot-Widget") ) : ?>         <?php endif; ?>
<?php } $postnum++; ?>

Here's what my page looks like now http://carrenomedia.com/rp

Here's the link to code for the Recent/Popular posts widget http://pastebin.com/YbMhKAMV

share|improve this question
    
what is the exact code of the 'popular posts' and 'recent posts' widgets? they might contain some sort of loop code as well which will interfere with your loop. –  Michael Aug 11 '12 at 18:18
    
@Michael I linked it above for you pastebin.com/YbMhKAMV –  Robert Aug 11 '12 at 18:31
    
Looks like it does have to do with the Recent/Popular post widget as when I removed it, there were no duplicates. I just have to figure out what code is messing it up in the widget. –  Robert Aug 11 '12 at 18:35

1 Answer 1

I think you should use the wp_reset_postdata(); function after the widget area.

Probable reason for this behavior is the widget, which itself uses two wp_query(); functions. May be those quires modifying the global $post data which is actually used by your main query.

I guess calling wp_reset_postdata(); function right after the widget will solve the problem.

Reference - Codex wp_reset_postdata()

After looping through a separate query, this function restores the $post global to the current post in the main query.

share|improve this answer

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.