1

I want to break the loop into sections of posts so I can put content in between that doesn't repeat. Is it possible to end the loop and restart?

My code works for my layout works if there are 10 posts on a page. When I have fewer it breaks because the closing div for .grid is tied in with the 10th post showing.

This shows my layout: 1st post, 2-5 in a grid, a non-post div separating, repeat of post then posts in a grid. layout

        <?php if ( have_posts() ) :
        $count = 0;

        while ( have_posts() ) : the_post();
            $count++; 

            if ($count == 1) : ?>
                <article class="large"></article>

            <?php elseif ($count == 2) : ?> 
                <div class="grid"> 
                    <article></article>
            <?php elseif ($count == 3) : ?>     
                <article></article>
            <?php elseif ($count == 4) : ?>  
                <article></article>
            <?php elseif ($count == 5) : ?>     
                <article></article>
            </div> <!-- .grid -->   

            <div id="edit">
            </div> <!-- #edit -->

            <?php elseif ($count == 6) : ?>
            <article class="large"></article>

            <?php elseif ($count == 7) : ?> 
                <div class="grid"> 
                    <article></article>
            <?php elseif ($count == 8) : ?>     
                <article></article>
            <?php elseif ($count == 9) : ?>  
                <article></article>
            <?php elseif ($count == 10) : ?>    
                <article></article>
            </div> <!-- .grid -->

            <?php endif; ?>
        <?php endwhile; ?>

    <?php endif; ?>
  • how does your loop look now? edit your quest, please. – prosti Dec 21 '16 at 1:01
  • @prosti I added the code. What I have works, but it ties the special middle div and the ends of the containing class="grid" divs in with posts so the layout breaks if not a full page of posts. – BethA Dec 21 '16 at 1:24
  • @Nath That restarts the loop, but I mean from the last post. That starts from post 1 but I need to start from the last post in the previous section. – BethA Dec 21 '16 at 1:40
  • @Nath Sorry, I said that was wrong, but it works! – BethA Dec 21 '16 at 1:45
0

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;
?>
| improve this answer | |
-1

You can use before the while loop

global $wp_query;
$total_posts = $wp_query->post_count;

To get the number of the posts you will get since not always you will get 10.

Now, $total_posts can help you create the custom logic you need.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.