The other day, whilst merrily making a website for an ensemble called Tesserae, I needed to figure out how to display my posts in 2 columns, side by side. In this case, the posts were a custom post type biographies that I used to display the various members biographies on the site.
It wasn’t hard to do, here’s the code I used to display half of the posts:
<?php // Get the number of posts in the custom post type 'biographies' $count = wp_count_posts( 'biographies' ); ?>
Let’s say the total number of posts is 7.
<?php // Divide the number of published posts by 2 and round up in case we get a fraction $num_biogs = ceil( $count->publish / 2 ); ?>
This would give us 4. We must round up this number otherwise, we’d get 3.5 and that’s no good!
<?php // Now use this number for the showposts parameter in our query $biogs = new WP_Query( 'post_type=biographies&showposts=' . $num_biogs ); while ( $biogs->have_posts() ) : $biogs->the_post(); // the_title(); the_content(); etc endwhile; wp_reset_query(); ?>
And the remaining posts:
<?php // Now use the variable $num_biogs as the offset parameter in another query $biogs = new WP_Query( 'post_type=biographies&showposts=-1&offset=' . $num_biogs ); while ( $biogs->have_posts() ) : $biogs->the_post(); // the_title(); the_content(); etc endwhile; wp_reset_query(); ?>
Obviously this is only the PHP, and you’re going to need some HTML there to actually use this! I just floated a couple of <div>s side by side, then popped the first block of code inside the left-hand <div> and the 2nd chunk in the 2nd <div>.
You can see the finished product at http://tesserae-la.com/who/