Single Query Running Multiple Loops, repeating posts
-
Hey everyone, so I’m trying to reduce the number of queries on my home page by running several loops in the same query. For whatever reason, each new loop is running the initial post…
So loop 1, 2, 3, and 4 are each displaying post 0 instead of 0, 1, 2, 3, 4 and so on…
here’s my set up, is my theory and set up wrong? or am I missing something rather important?
<?php $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1; $args = array( 'posts_per_page' => 1, 'paged' => $paged, ); $featured = new WP_Query($args); if ($featured->have_posts()) : while ($featured->have_posts()) : $featured->the_post(); $do_not_duplicate = $post->ID; ?> ?> <?php get_template_part('front/top'); ?> <?php endwhile; ?> <?php endif; ?> <?php rewind_posts(); ?> <?php get_template_part('front/top-stories'); ?> <div class="container"><section class="row front"> <section class="col-lg-9 col-md-12 col-sm-12 featured-post"> <?php if ($featured->have_posts()) : while ($featured->have_posts()) : $featured->the_post(); ?> <?php get_template_part('front/featured-loop-1'); ?> <?php endwhile; ?> <?php endif; ?> <?php rewind_posts(); ?> <?php if ($featured->have_posts()) : while ($featured->have_posts()) : $featured->the_post(); ?> <?php get_template_part('front/featured-loop-2'); ?> <?php endwhile; ?> <?php endif; ?> <?php rewind_posts(); ?> <?php if ($featured->have_posts()) : while ($featured->have_posts()) : $featured->the_post(); ?> <?php get_template_part('front/featured-loop-1'); ?> <?php endwhile; ?> <?php endif; ?> <?php rewind_posts(); ?> </section> <?php get_template_part('front/featured-loop-3'); ?> </section></div>
here are my actual loops, loop 1:
<?php $backgroundImg = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full' ); ?> <div id="post-<?php the_ID(); ?>" <?php post_class('container-fluid black-bg'); ?> style="background-image: url('<?php echo $backgroundImg[0]; ?>'); background-repeat: no-repeat; background-position: center 65%; background-blend-mode: overlay;"> <section class="row"> <article id="top-front-wrapper" class="col-sm-12 col-md-12 col-lg-7 col-12 top-post text-white offset-lg-1 offset-0"> <h1 class="big-text"> <a href="<?php the_permalink();?>"><?php the_title(); ?> </a> </h1> <?php get_template_part('citation-featured'); ?> </article> <!-- No posts found --> </section></div>
<div id="post-<?php the_ID(); ?>" <?php post_class('row'); ?>> <header class="col-lg-6 col-md-6 col-sm-12 col-12 order-lg-2 featured-content"> <?php get_template_part('cover-front'); ?> </header> <article class="col-lg-6 col-md-6 col-sm-12 col-12 order-lg-1 featured-content"> <?php get_template_part('headline'); ?> <?php get_template_part('citation'); ?> <?php html5wp_excerpt('html5wp_index'); // Build your custom callback length in functions.php ?> <?php edit_post_link(); ?> </article> </div>
<div class="col-lg-3 col-sm-12 col-md-12 double featured-post offset-lg-0 offset-0"> <?php $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1; $args = array( 'posts_per_page' => 3, 'paged' => $paged ); $my_posts = new WP_Query($args); if ($my_posts->have_posts()) : while ($my_posts->have_posts()) : $my_posts->the_post(); $featured_side = $post->ID; ?> <div id="post-<?php the_ID(); ?>" <?php post_class('featured-content row bottoms'); ?>> <div class="col-lg-12 col-md-6 col-sm-6"> <?php get_template_part('cover-front'); ?> </div> <div class="col-lg-12 col-md-6 col-sm-6"> <h2 class="small-text"><a href="<?php the_permalink();?>" title="<?php the_title(); ?>"><?php the_title(); ?> </a></h2> <?php get_template_part('citation'); ?> <?php edit_post_link(); ?> </div> </div> <?php endwhile ?> <?php endif ?> <?php wp_reset_query; ?> </div>
Thanks in advance! Appreciate any and all support
Viewing 6 replies - 1 through 6 (of 6 total)
Viewing 6 replies - 1 through 6 (of 6 total)
- The topic ‘Single Query Running Multiple Loops, repeating posts’ is closed to new replies.