I've created a PHP while statement that displays 8 posts from WordPress in a particular layout. Now I would like to keep running this loop until all the posts are displayed. So what I've done is created a variable called posts_displayed, and I'm using that to set the post_offset and incrementing it each time a post is displayed. The original code worked as expected, but when I added the additional while statement around it, no posts are displayed.
I've updated the code such that there is one main query and loop with four different queries and loops within. The problem I am encountering now is on line 131, where I am getting an unexpected endwhile error.
<?php $posts_displayed = 0; ?>
<?php
$main_query = new WP_Query( array(
'category_name' => 'Travel',
));
?>
<?php if ( $main_query->have_posts() ) : ?>
<?php while ( $posts_displayed < ($main_query->post_count) ) ?>
<?php
$first_query = new WP_Query( array(
'category_name' => 'Travel',
'posts_per_page' => 2,
'offset' => $posts_displayed,
));
?>
<div class="row row__padding--bottom homepage__row--one">
<?php if ( $first_query->have_posts() ) : ?>
<?php while ( $first_query->have_posts() ) : $first_query->the_post(); ?>
<div class="col-sm-12 col-md-6">
<div class="journal__latest" style="background: url(<?php echo get_the_post_thumbnail_url( $post_id, 'large' ); ?>) !important; background-size: cover !important; background-position: center center !important; background-repeat: no-repeat !important;">
</div>
<div class="post__info--container">
<a href="<?php echo esc_url( get_permalink()); ?>"><h4><?php the_title(); ?></h4></a>
</div>
</div>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
</div>
<?php
$second_query = new WP_Query( array(
'category_name' => 'Travel',
'posts_per_page' => 3,
'offset' => posts_displayed,
));
?>
<div class="row row__padding--bottom">
<?php if ( $second_query->have_posts() ) : ?>
<?php while ( $second_query->have_posts() ) : $second_query->the_post(); ?>
<div class="col-12 col-md-4">
<div class="portfolio__featured" style="background: url(<?php echo get_the_post_thumbnail_url( $post_id, 'large' ); ?>) !important; !important; background-size: cover !important; background-position: center center !important; background-repeat: no-repeat !important;">
</div>
<div class="post__info--container">
<a href="<?php echo esc_url( get_permalink()); ?>"><h4><?php the_title(); ?></h4></a>
</div>
</div>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
</div>
<?php
// the query
$third_query = new WP_Query( array(
'category_name' => 'Travel',
'posts_per_page' => 1,
'offset' => posts_displayed,
));
?>
<div class="row row__padding--bottom">
<?php if ( $third_query->have_posts() ) : ?>
<?php while ( $third_query->have_posts() ) : $third_query->the_post(); ?>
<div class="col-12">
<div class="journal__featured" style="background: url(<?php echo get_the_post_thumbnail_url( $post_id, 'large' ); ?>) !important; !important; background-size: cover !important; background-position: center center !important; background-repeat: no-repeat !important;">
</div>
<div class="post__info--container">
<a href="<?php echo esc_url( get_permalink()); ?>"><h4><?php the_title(); ?></h4></a>
</div>
</div>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
</div>
<?php
// the query
$fourth_query = new WP_Query( array(
'category_name' => 'Travel',
'posts_per_page' => 2,
'offset' => posts_displayed,
));
?>
<div class="row row__padding--bottom">
<?php if ( $fourth_query->have_posts() ) : ?>
<?php while ( $fourth_query->have_posts() ) : $fourth_query->the_post(); ?>
<div class="col-sm-12 col-md-6">
<div class="journal__featured" style="background: url(<?php echo get_the_post_thumbnail_url( $post_id, 'large' ); ?>) !important; !important; background-size: cover !important; background-position: center center !important; background-repeat: no-repeat !important;">
</div>
<div class="post__info--container">
<a href="<?php echo esc_url( get_permalink()); ?>"><h4><?php the_title(); ?></h4></a>
</div>
</div>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
</div>
<?php endwhile; ?>
<?php else : ?>
<p><?php __('No News'); ?></p>
<?php endif; ?>