A two column loop with one lead post

I got a loop from internet (I forgot the site) for two column post view. It has a nice CSS for it too. But skipping that for the question now.

<?php $counter = 1; ?> 
                <?php query_posts( 'cat=3&showposts=6' ); //Find whether the catID is 3 ?>
            <?php while ( have_posts() ) : the_post() ?>  
                <?php /* Two Column Support get class */ ?> 
            <?php $class = ( $counter % 2  ? ' one' : '' ) ?> 
                <?php /* Two Column output open the div class */ ?> 
            <div class="column<?php echo $class;// echo $first; ?>"> 
            <?php news_format(); ?>
            </div> <!-- End Two Column Support close div class -->
<?php $counter++; //Update counter ?>

It printed my posts into 2 columns like:

Read More
=========
| 1 | 2 |
---------
| 3 | 4 |
---------
| 5 | 6 |
=========

It’s fine.
But I want to print it like this:

=========
|   1   |
---------
| 2 | 3 |
---------
| 4 | 5 |
---------
| 6 | 7 |
=========

What the modification I need to do?
I can’t pass any custom limit into the WordPress’ custom loop <?php while ( have_posts() ) : the_post() ?>. It’s for a custom theme. I did it, but the problem is, it’s showing like:

=========
|   1   |
---------
| 1 | 2 |
---------
| 3 | 4 |
---------
| 5 | 6 |
=========

Here is my whole bunch of code.

Related posts

Leave a Reply

1 comment

  1. 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()?