A two column loop with one lead post

Add a last test for 1 === $counter. And put that into a function in your theme’s functions.php like this:

/**
 * Alternating output.
 *
 * @param  string $first  Returned on first call.
 * @param  string $odd    Returned on every odd call.
 * @param  string $even   Returned on every even call.
 * @return string
 */
function first_odd_even( $first="first", $odd = 'odd', $even = 'even' )
{
    static $counter = -1;
    $counter += 1;

    if ( 0 === $counter )
        return $first;

    if ( $counter % 2 )
        return $odd;

    return $even;
}

Now in the loop you just call that function where you need it:

<?php 

query_posts( 'cat=3&showposts=6' ); //Find whether the catID is 3

while ( have_posts() ) : the_post()
?>
<div class="column<?php echo first_odd_even(); ?>"> 
<?php news_format(); ?>
</div>

You are allowed to use line breaks in PHP code. And space wherever it helps readability. 😉

And I recommend not to use query_posts(). Read: When should you use WP_Query vs query_posts() vs get_posts()?

Leave a Comment