WordPress.org

Support

Support » Themes and Templates » [Resolved] Adding a clearing div to every third post in the loop

[Resolved] Adding a clearing div to every third post in the loop

  • 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

Viewing 3 replies - 1 through 3 (of 3 total)
  • 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.

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

    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>';
    }
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘[Resolved] Adding a clearing div to every third post in the loop’ is closed to new replies.