Multi-Column Grid-Style Posts in WordPress

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

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(); ?>
// code to display a number of posts in multi columns top-to-bottom left-to-right
<?php global $query_string; ?>
$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;
// 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;?>">
$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 -->
<a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h2>
<div class="storycontent">
<?php the_excerpt(__('(more...)')); ?>
<!-- end of anything to do with post -->
</div> <!-- end #post -->
endforeach; ?>
<?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>
<?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.

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


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;


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.

WordPress as a Model of Human Consciousness

matrixMany researchers of the human mind have already tried to explain some aspects of the workings of the mind and ego by analogy of a computer.

This holds true also for an open source program such as wordpress.

I will put it this way: the wordpress software, ie. the core program, is the subconsciousness. We know it is there, but generally we are not aware of it and don’t understand how it works. And, unless something goes wrong, we don’t really want to know.

And in a way, you could see that all the programmers and organizers of  as an open source software  are a kind of  ‘creator’ who is well above all and who has put all this out for us to work with. This is not unlike some interpretation of G.O.D. (greater organized design) who made us but also gave us the freedom and intelligence to change the program.

Now, there a many many thousands of blogs out there, using software to program there own way of expression.

What every blog software does, is to take a lot of input in (the posts, pages, articles, pictures and so) and arrange and present them in a way that is personal and special for the individual (programmer).
Continue reading “WordPress as a Model of Human Consciousness”