WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Adding a clearing div to every third post in the loop (4 posts)

  1. danielddb
    Member
    Posted 2 years ago #

    Hi there, I need to add a clearing div to the end of every third post in the loop (three posts per row).

    It's currently working using

    <?php
    if (0 == $count%3) {
    echo '<div class="clear"></div>"';
    }
    ?>

    However, if a third post isn't there, the clearing div won't appear which is not what I want.

    Say for instance, I only have one post in a row, how would I add the clearing div and the same question if I only have two posts in a row.

    Any help is very much appreciated,

    Thanks,

    Dan

  2. Chris Marslender
    Member
    Posted 2 years ago #

    if ( have_posts() ) : while( have_posts() ) : the_post();
        //Loop code goes here. Blah Blah.
        //Your code from above goes next
        if ( 0 == $count%3 ) {
            echo 'div class="clear"></div>';
        }
    endwhile; //ending the loop
    if ( 0 != $count%3 ) {
       echo 'div class="clear"></div>';
    }

    This basically just performs the reverse check of what you were checking inside the loop. So if the last post didnt trigger the clearing div, it will right after the while function is done running.

  3. danielddb
    Member
    Posted 2 years ago #

    It works perfectly! I've literally been trying to solve this for hours so thanks alot for taking the time to help me out!

  4. Bruz
    Member
    Posted 1 year ago #

    If this isn't working for someone, just add "$count++;" after the_post();. This work for me.

    So:

    if ( have_posts() ) : while( have_posts() ) : the_post(); $count++;
        //Loop code goes here. Blah Blah.
        //Your code from above goes next
        if ( 0 == $count%3 ) {
            echo 'div class="clear"></div>';
        }
    endwhile; //ending the loop
    if ( 0 != $count%3 ) {
       echo 'div class="clear"></div>';
    }

Topic Closed

This topic has been closed to new replies.

About this Topic