Adam Azad Adam Azad - 3 months ago 8
PHP Question

If Loop has odd number of posts on last page then Custom style for last post in it

My loop displays posts in two columns using this:

<?php
if (have_posts()): while (have_posts()) : the_post();
$count++;
?>
<?php if ($count == 1) : ?>
<div class="home-ci-row">

<div style="padding: 0px;" class="main-column-item-wrap">
CONTENT OF POST : Title, Thumbnail, Excerpt... etc
</div>

<div class="home-ci-gap"></div><!-- /* the gap */ -->

<?php elseif ($count == 2) : ?>

<div style="padding: 0px;" class="main-column-item-wrap">
CONTENT OF POST : Title, Thumbnail, Excerpt... etc
</div> <!-- main-column-item-wrap -->


</div><!-- /* home-ci-row*/ -->

<?php $count = 0; ?>

<?php else : ?>
// No posts
<?php endif; endwhile; endif; ?>


You can see that the
<div class="home-ci-row">
opens in the first count & closes in the second one
</div>


so when my loop has an even number of posts works great, but with odd number it doesn't close the div

so My idea is this: If loop has even number

http://i.stack.imgur.com/Hu4Ua.png

If loop has odd number of posts

http://i.stack.imgur.com/JjqzZ.png

Answer

This question was answered on WP Development StackExchange by fischi

Quoting from his answer:

You could do this much easier. As you are making a layout that can be achieved by floats, there is no need to declare a Row every second time.

In my Code example I just youse the $count to determine the Class of the HTML-Element. In combination with the total Number of Posts displayed.

If the total number of posts $wp_query->post_count is reached by the $count AND the total number is odd, I give the Element the Class fullwidth. In the same way, I determine if it is the first or the second (see the IF-Statement).

All I need to do afterwards is output the corresponding Class for each HTML-Element in the Loop. Besides the Class, no Element is diffferent from each other.

I use the Modulo Operator in PHP ( % ) to determine odd/even. It delivers 1 if I use $count % 2 and $count is odd. If you are not sure about this operator, read about it here.

So your code could look like this:

<?php
    $count = 0;
    if (have_posts()): while (have_posts()) : the_post();
        if ( ++$count == $wp_query->post_count && ( $wp_query->post_count % 2 ) == 1 ) {
            // if final count is reached AND final count is odd
            // full width item
            $postclass = "fullwidth";
            $opentag = '';
            $closingtag = '</div>';
        } else if ( ( $count % 2 ) == 1 ) {
            // if $count is odd it is the first item in a 'row'
            $postclass = "halfwidth first";
            $opentag = '<div class="home-ci-row">';
            $closingtag = '';
        } else {
            // second item in a row
            $postclass = "halfwidth second";
            $opentag = '';
            $closingtag = '</div>';
        }
?>
    <?php echo $opentag; ?>
    <div class="main-column-item-wrap <?php echo $postclass; ?>">
    CONTENT OF POST : Title, Thumbnail, Excerpt... etc
    </div><!-- main-column-item-wrap -->
    <?php echo $closingtag; ?>

<?php endwhile; endif; ?>
Comments