Want to separate sections of posts. Can you restart loop?

This pattern can be reduced to two main sections, large box and grid boxes.

And you said you were having trouble closing the grid boxes when your posts are less than 10, an easy way to close off the grid would be to check if the current post is equal to the total posts.

Here is my interpretation of your loop:

<?php
if (have_posts()) :
    while (have_posts()) :
        the_post();
        $total_posts = $wp_query->post_count;
        $current_post = $wp_query->current_post;

        // Large boxes
        if (in_array($current_post, array(1, 6))) : ?>

            <article class="large"></article>
    <?php
        elseif ($current_post > 1 && $current_post < 6 || $current_post > 6) :
            // Open grid box
            in_array($current_post, array(2, 7)) ? "<div class="grid">" : "";
    ?>
            <article></article>
    <?php
            // Close grid box
            in_array($current_post, array(6, $total_posts)) ? "</div> <!-- .grid -->" : "";
            // Add seperator
            $current_post == 6 ? "<div id='edit'></div> <!-- #edit -->" : "";

        endif;

    endwhile;
endif;
?>

Leave a Comment