Answers for "Wordpress even odd post count"

0

Wordpress even odd post count

<?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; ?>
Posted by: Guest on June-22-2021

Browse Popular Code Answers by Language