Multi-Column Grid-Style Posts in WordPress

RECOMMENDATION:
as this code can have its challenges, and is not always easy to apply, i would like to point to an easier to implement approach with the same results (depending on formatting):
Playing with columns – stacking posts in a grid.
There is also a newer article for creating posts in three columns.

Following question comes up regularly in the wordpress support forum: ‘i want to show my posts in three (four, five) columns, how can i do it?’

see the effect here in action: under pages ‘grid of posts’.

here, i am going  to present yet another solution: a flexible core structure to allow any number of columns with any number of rows, limited only by screen space, readability and fantasy…

a three column grid category template
a three column grid category template

the structure (for instance 3 columns, 4 rows, 12 or more posts):

 
1  5   9
2  6  10
3  7  11
4  8  12

the structure (for instance 3 columns, 4 rows, less than 12 posts, i.e. 7):

 
1  4  6
2  5  7
3 

the code is setup step by step:

  1. setting of variables
  2. query_posts
  3. calculate rows
  4. loop 1 – the columns
  5. the wordpress loop – the rows
  6. page navigation

here is the full code:

<?php get_header(); ?>
<?php
// code to display a number of posts in multi columns top-to-bottom left-to-right
?>
<?php global $query_string; ?>
<?php
$set_number_of_columns = 2; // enter numbers of columns here;
$set_number_of_rows = 4; // enter numbers of rows here
$set_showpost = $set_number_of_columns * $set_number_of_rows ;
//setup query with parameter for paged
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts($query_string.'&showposts='.$set_showpost.'&paged='.$paged);
// get number of posts for this,
$num_of_posts = $wp_query->post_count;
// make adjustment for paged to get three equaly long columns even on last paged page
// divide by $set_number_of_columns to get number per column,
// round up to next integer to make $ppc posts per column variable, or $set_number_of_rows whatever is smaller
$ppc = min(ceil($num_of_posts/$set_number_of_columns),$set_number_of_rows);
// calculates number of rows, i.e. showposts for the following query_posts and get_posts
?>
<?php for ( $col = 1; $col <= $set_number_of_columns; $col += 1) { ?>
<div class="col<?php echo $col;?>">
<?php
$row = 1;
$noffset = ($col -1) * $ppc + ($paged - 1) * $set_showpost ; //calculate offset parameter if paged
$posts = get_posts($query_string.'&numberposts='.$ppc.'&offset='.$noffset);
foreach ($posts as $post) : start_wp(); ?>
<div id="post-<?php the_ID(); ?>" class="post row-<?php echo ($row%2); ?>">
<!-- start of anything to do with post -->
<h2>
<a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h2>
<div class="storycontent">
<?php the_excerpt(__('(more...)')); ?>
</div>
<!-- end of anything to do with post -->
</div> <!-- end #post -->
<?php
$row++;
endforeach; ?>
</div>
<?php } ?>
<?php // close the for-loop // ?>
<div class="navigation">
<div class="alignleft"><?php next_posts_link('&laquo; Older Entries') ?></div>
<div class="alignright"><?php previous_posts_link('Newer Entries &raquo;') ?></div>
</div>
<?php get_footer(); ?>

the naming of the css classes is:
class .col1 for the first column; class .col2 for the second column; and so forth.
same thing for the rows: class .row1 for the posts in first row, on so on.
going with this code is the css:

.col1, .col2 {float:left; width:49%; }

you can see the code in action: under pages ‘grid of posts’.
the effect ia achieved by giving each post a fixed height; and using a bit of javascript trickery.

Inspirational Art on Silk

one million dollars, copyright silkandart
one million dollars, copyright © silkandart a source of inspirational art on silk

Good Feng Shui – an auspicious symbol of wealth – one million dollars.

An original silk painting by SilkAndArt.com; a nice must-have feature for any office and home.

Silk & Art provides individual artwork on silk, as well as wearable inspired art.

‘Silk & Art embodies beauty and luxury combined in exquisite, handpainted silk fabric.’

More-Than-Zebra style wordpress loop

to start, i would like to express my thanks and gratitude to all the volunteers in the wordpress forum who patiently and freely give their time and attention, and share their knowledge and experience with all the users coming with questions and problems about their wordpress installation and modifications.
a great community.

lists or posts styled with alternating color backgrounds, for instance, can inprove the readability of blogs.

most themes and the standard wordpress installation are not offering this feature.
however, it is easy to implement, when you know how.

for example we could give alternating posts within the ‘wordpress loop’ a different extra class (‘odd’ or ‘even’) which can then be used with css.
to begin with, we introduce a counter ($c = 0;) before the loop, increment it inside the loop ($c++), and generate a class depending on the number of the counter by checking the modulus 2 of the number ($c % 2 – modulus is the remainder of the division):

<?php $c = 0 ; ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php $c++;
if( $c % 2 == 0 ) $extra_class = ' even';
else $extra_class = ' odd'; ?>
<div class="post <?php echo $extra_class; ?>">
<!--typical stuff in the loop-->
</div>
<?php endwhile; ?>
<?php else : endif; ?>

the class ‘odd’ or ‘even’ can be used in the style.css to create different background colors, alignments, margins, paddings, font-styles – no limits.

the most basic application of this method with a counter is how to style the first post in the loop different (only showing the logic part of the code):

<?php $c++;
if( $c == 1 ) $extra_class = ' first';
else $extra_class = ''; ?>

to make it more interesting, the next example is to create three different styles and a special style for the last post on the page.
we can get the number of the last post with

$wp_query->post_count 

The structure is the same, the main difference is in the ‘if, elseif, else’ logic:

<?php $c++;
if( $c == $wp_query->post_count ) $extra_class = ' last';
elseif( $c % 3 == 0 ) $extra_class = ' three';
elseif( $c % 3 == 2 ) $extra_class = ' two';
elseif( $c % 3 == 1 ) $extra_class = ' one';
?>

you can extend this method to create any number of cyclic classes.

EDIT: a new way of adding any number of recursive css classes – without counter variable, using the $wp_query->current_post of a default loop:

$extra_classes = array('one','two','three','four','five');
$extra_class = $extra_classes[$wp_query->current_post%count($extra_classes)];
echo $extra_class;

END OF EDIT

and you can use it in any situation in your wordpress templates, where you loop through some output – lists, foreach, for, while, posts, comments, …

special thanks to @esmi who dared venturing into the realm of more than two alternating styles, and who inspired me to post this article.

WP Kubrick default theme in 3 columns

Part of my time, I am spending designing and troubleshooting wordpress themes.

3 column kubrick theme

This is my latest ‘hack’, turning the default theme that comes with every wordpress installation, into a 3 column version, with equal sized left and right sidebar columns, all dynamic and widget ready.

All functions of the default theme are fully preserved. You can still modify the header colours from within the dashboard.

All files and graphics zipped for download. A great start for learning to mod your own theme.

At last a ‘default’ wordpress theme that fills the width of my laptop’s monitor 😉

Update: horizontal page navigation below the header included.

To activate it, edit header.php and  remove the commenting where indicated.