1

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.

2
  • 2
    Any reason why this question is still open? :-) Would be nice to see it done and dusted Commented Aug 11, 2014 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) Commented Aug 11, 2014 at 16:32

1 Answer 1

2

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

3
  • 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. :( Commented Nov 7, 2012 at 7:45
  • Anyways thanks for the query_posts() and the code break-up suggestion. ;) Commented Nov 7, 2012 at 8:04
  • @MayeenulIslam See my update with a better variant of that code. Tested in TwentyEleven. Commented Nov 8, 2012 at 2:16

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.