Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End WordPress / PHP Close loop if no new item/post exists

  • This topic is empty.
Viewing 1 post (of 1 total)
  • Author
    Posts
  • #167050
    Jeremy Englert
    Participant

    I have created a WordPress loop that that wraps wraps a div around every 3 sets of blog posts. Essentially, it outputs like this:

    <div> // This row does have 3 sets of columns, so it will create another row
        <div>
            <--Content Stuff-->
        </div>
        <div>
            <--Content Stuff-->
        </div>
        <div>
            <--Content Stuff-->
        </div>
    </div>
    <div> // Since this row doesn't have three sets of columns, so it does not create another row
        <div>
            <--Content Stuff-->
        </div>
        <div>
            <--Content Stuff-->
        </div>
    </div>
    

    However, I run into issues when I have 3, 6 or 9 blog posts as the loop is creating a new div but has no content to fill it with.

    Here is what I currently have – which is working great except for the issue noted above:

    <div>
        <?php $i = 1; ?>
        <?php query_posts; ?>
        <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
        <?php if ($community_posts->have_posts()) : while ($community_posts->have_posts()) : $community_posts->the_post(); ?>
    
    <div>
        <?php get_template_part( 'partials/loop', 'archive-grid' ); ?> // This is the WP Loop
        <?php if ($counter % 3 == 0){echo '</div><div>';} ?>
        <?php $counter++ ; endwhile; echo '</div>'; ?>
    </div>      
    
    <?php else : ?>
        <?php get_template_part( 'partials/content', 'missing' ); ?>
    <?php endif; ?>
    
Viewing 1 post (of 1 total)
  • The forum ‘Back End’ is closed to new topics and replies.