Gary Woods
Gary Woods

Reputation: 1011

Second loop inside The Loop messing up the post

In my Wordpress post page, I have added a second loop that displays related posts. This loop is inside the main loop that shows the content of the post. The problem is that the addition of the second loop is messing up everything on the page.

The comments are from a different post, and making a new comment makes you jump to a different page and so on.

Obviously, I have not inserted the second loop correctly in the main loop.

Here is my code:

<div id="entries">

    <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
        <div class="entry post postpage clearfix">
            <?php if (get_option('aggregate_integration_single_top') <> '' && get_option('aggregate_integrate_singletop_enable') == 'on') echo(get_option('aggregate_integration_single_top')); ?>

            <h1 class="title"><?php the_title(); ?></h1>

            <?php get_template_part('includes/postinfo','single'); ?>

            <?php the_content(); ?>



<!-- START SECOND LOOP -->
<div class="radarcont">

<?php
$category = get_the_category();
$current_category = $category[0]->term_id;
$qarr = array(
    'showposts' => '4',
    'cat' => $current_category,
    'tag' => 'pinned', // filtered tag
    'post__not_in' => array(get_the_ID())
);
$q = new WP_Query($qarr);
if($q->have_posts()) : while ($q->have_posts()) : $q->the_post();
    ?>
<div class="singleradar clearfix">
    <?php
    $thumb = '';
    $width = 130;
    $height = 75;
    $classtext = 'post-image';
    $titletext = get_the_title();
    $thumbnail = get_thumbnail($width,$height,$classtext,$titletext,$titletext,false,'Recent');
    $thumb = $thumbnail["thumb"];
    ?>

    <?php if($thumb <> '' && get_option('aggregate_thumbnails_index') == 'on') { ?>
    <div class="thumb">
        <a href="<?php the_permalink(); ?>">
            <?php print_thumbnail($thumb, $thumbnail["use_timthumb"], $titletext, $width, $height, $classtext); ?>
            <span class="overlaymed"></span>
        </a>
    </div>  <!-- end .post-thumbnail -->
    <?php } ?>

    <h3 class="title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
</div> <!-- end .block-post -->

<?php endwhile; endif;?>

</div> <!-- end .radarcont -->
<!-- END SECOND LOOP -->



        </div> <!-- end .entry -->          

        <?php if (get_option('aggregate_show_postcomments') == 'on') comments_template('', true); ?>

    <?php endwhile; // end of the loop. ?>

How can I fix this? How can I adjust the second loop so that it does not mess up the rest?

Upvotes: 0

Views: 213

Answers (1)

Zvonko Biskup
Zvonko Biskup

Reputation: 393

After looping through the second loop, you must reset your post data with:

wp_reset_postdata();

More info on: http://codex.wordpress.org/Function_Reference/wp_reset_postdata

Upvotes: 5

Related Questions