Forums

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

  1. danielddb
    Member
    Posted 4 months 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. cmmarslender
    Member
    Posted 4 months 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 4 months 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!

Reply

You must log in to post.

About this Topic