Anne
Anne

Reputation: 111

wordpress loop returning full content outside of loop

I'm using a basic wordpress loop to show 3 blog posts + pagination, using query_posts to loop these - code attached. The loop and pagination work well, but it's showing a strange glitch: the full page content (for all 3 posts) is also looped and displayed. The full content appears after the pagination and after the endwhile closing tag, at the end of all code.

My suspicion is that the loop is somehow not closed, but any closing tags I know to check are already present. My php is still early beginner level and mostly focused on standard wordpress functions, so any suggestions how I can close this up would be welcome.

Note: the loop is using query_posts instead of new WP_Query because of the pagination, this did not work using any new WP_Query loops. I want to get rid of the additional content but keep the base loop the same so the pagination is still functional.

Final note: I have verified the extra content is caused by this loop as removing the loop also removed the content.

<article class="row">
        <?php query_posts(array('posts_per_page' => 3, 'post_type' => 'post', 'paged' => get_query_var('page')));
        if (have_posts()) : while (have_posts()) : the_post(); ?>
          <a href="<?php the_permalink(); ?>">
                <?php the_post_thumbnail(); ?>
                <h3><?php the_title(); ?></h3>
                <?php the_excerpt(); ?>
                <span class="inline-button">Read more <i class="fas fa-chevron-right"></i></span>
          </a>
      <?php endwhile; ?>
  <?php endif;?>
  <?php wp_reset_postdata(); ?>
  </article>

<article class="row">
        <?php   the_posts_pagination(); ?>
</article>


<!------ in html, all extra generated content will appear here below the above article ---> 

Upvotes: 1

Views: 75

Answers (1)

Anne
Anne

Reputation: 111

Resolved!

I found the answer and simply closed it adding this to the end:

<?php wp_reset_query() ;?>

Upvotes: 1

Related Questions