Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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:

=========
| 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.

share|improve this question
2  
Any reason why this question is still open? :-) Would be nice to see it done and dusted –  Pieter Goosen Aug 11 at 12:59
    
@PieterGoosen: Thanks for pointing it out. I'll sort it out very soon if possible (I'm a bit busy with some family chores) –  Mayeenul Islam Aug 11 at 16:32

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

share|improve this answer
    
Oops! it's not working for me. Till showing 1, 1, 2, 3... Though the CSS is broken, no matter, but the main purpose is NOT served. :( –  Mayeenul Islam Nov 7 '12 at 7:45
    
Anyways thanks for the query_posts() and the code break-up suggestion. ;) –  Mayeenul Islam Nov 7 '12 at 8:04
    
@MayeenulIslam See my update with a better variant of that code. Tested in TwentyEleven. –  toscho Nov 8 '12 at 2:16

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.