0

I have a code which wraps every 4 posts in div. How can I adapt it to wrap every month posts in div.

<?php
$args = array(
    'post_type' => 'posts',
    'posts_per_page' => -1,
    'order' => 'DESC'
);

$the_query = new WP_Query($args);
if ($the_query->have_posts()) :
    $counter = 0;
    while ($the_query->have_posts()) : $the_query->the_post();
        if ($counter % 4 == 0) :
            echo $counter > 0 ? "</div>" : ""; 
            echo "<div class='row'>";
        endif;
        ?>
        <div class="col-3">
            <?php the_title(); ?>
        </div>
        <?php
        $counter++;

    endwhile;
endif;
wp_reset_postdata();
?>
  • 1
    Instead of counting to 4 to echo your wrapper, grab the month from each post and compare it to the previous post. When it changes, echo your wrapper markup, and set the current post's month as the new $current_month – jdm2112 Nov 28 '16 at 21:42
2
$last_month = null;

while ( $the_query->have_posts() ) :
    $the_query->the_post();
    $the_month = get_the_time( 'Ym' ); // e.g. 201611

    if ( $last_month !== $the_month ) {
        if ( $last_month !== null ) {
            // Close previously opened <div class="row" />
            echo '</div>';
        }

        echo '<div class="row">';
    }

    $last_month = $the_month;

    ?>

    <div class="col-3">
        <?php the_title(); ?>
    </div>

    <?php

    if ( $the_query->current_post + 1 === $the_query->post_count ) {
        // Last item, always close the div
        echo '</div>';
    }

endwhile;
| improve this answer | |
0

@TheDeadMedic Thank you for your help. Code works of course. I wanted also to style first group differently so I added post count. Heres full code:

<?php

    $args = array(
        'post_type' => 'post',
        'posts_per_page' => -1,
        'order' => 'DESC'
    );

    $the_query = new WP_Query($args);
    if ($the_query->have_posts()) :     

        $last_month = null;
        $postCount = 1;

        while ( $the_query->have_posts() ) : $postCount++;
            $the_query->the_post();
            $the_month = get_the_time( 'Ym' ); 

            if ( $last_month !== $the_month ) {
                if ( $last_month !== null ) {

                    echo '</div>';
                }

?>      

<?php if($postCount == 2) { ?>

        <div class="firstgroup"> // style first group of posts

<?php } else { ?>

        <div class="othergroups"> // style other groups of posts

<?php } ?>

<?php               

            }

            $last_month = $the_month;

            ?>

            <div>

                <?php the_title(); ?>

            </div>

            <?php

            if ( $the_query->current_post + 1 === $the_query->post_count ) {
                // Last item, always close the div
                echo '</div>';
            }

        endwhile;

    endif;

    wp_reset_postdata();

?>
| 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.