I’m hoping someone might be able to help me with multiple loops problems I’m having. I am trying to do a magazine style layout with a featured post in a hero style which is the latest blog post. I am then trying to get the rest of the blog posts (10 to page ) in a second loop.
I have put the featured blog post loop in my header for the blog home and get the right post.
In my blog posts home page blade I have the second loop getting the rest of the posts.
Both work but I have the following issues:
when using the offset in the second loop it screws up the pagination and just shows pages of duplicate posts ignoring the post per page setting
when trying the code in the wordpress codex where they use the $do_not_duplicate method - https://codex.wordpress.org/The_Loop#Multiple_Loops_in_Action. the second loop displays no posts. I have discovered that the $do_not_duplicate method is correctly getting the id of the featured post first loop, but when it comes to the second loop, the $do_not_duplicate variable is empty, the value is not being passed from partial to partial.
I am very new to Sage and I really don’t know how to pass that value from one blade to the next, or have the value be saved. Or indeed if there is another way to do it.
I read about the right way to run multiple WordPress loops forum post with the logic in the controller file but I haven’t a clue how to start with that with two loops and then getting the $do_not_duplicate value in there.
This is my first loop code:
@php $my_query = new WP_Query( 'posts_per_page=1' ); while ( $my_query->have_posts() ) : $my_query->the_post(); $do_not_duplicate = $post->ID; @endphp `
And my second loop:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); if ( $post->ID == $do_not_duplicate ) continue; ?> <div> <div class="uk-card uk-card-default uk-text-center"> <div class="uk-card-media-top"> <img src="<?php the_post_thumbnail_url(); ?>" data-src="" alt="" data-uk-img /> </div> <div class="uk-card-body"> <h3><a class="uk-link-reset" rel="bookmark" href="<?php the_permalink(); ?>"> <?php the_title(); ?></a></h3> @include('partials/entry-meta-main') <div class="uk-margin-small-top"> <?php the_excerpt(); ?> </div> <p><a class="uk-button red-button uk-border-rounded" href="<?php the_permalink(); ?>">Read more »</a></p> </div> </div> </div> @php endwhile; endif; @endphp
Would really appreciate any help anybody can offer.