21 | Multi-Column Grid-Style Posts in WordPress

Posted by alchymyth | Wordpress | Friday 29 January 2010

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

EDIT:
as this code can have its challenges, and is not always easy to apply, i would like to point to another approach with the same results:
Playing with columns – stacking posts in a grid.
END of EDIT

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

i’ll begin with a step by step introduction into the concept:

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

1. setting the variables:
$set_number_of_columns = 3; // enter numbers of columns here;
$set_number_of_rows = 3; // enter numbers of rows here
$set_showpost = $set_number_of_columns * $set_number_of_rows ;

2. query_posts:
global $query_string;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts($query_string.'&showposts='.$set_showpost.'&paged='.$paged);

3. calculate rows:
// make adjustment to get three equaly long columns even on last paged page
// get number of posts for this,
$num_of_posts = $wp_query->post_count;
// 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

4. loop 1 – the columns:
<?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 ?>

5. wordpress loop – the rows and posts:
<?php $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; ?>">
<?php //output all post related data, i.e. date, title, content/excerpt, postmetadata, tags, etc. // ?>
</div> <!--closing .post div -->

6. page navigation:
<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>

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.

tagged: . . . . . . . . . . . . . . .

13 Comments

alchymyth | June 27, 2010

hi Salsa,
well done, and thanks for the corrected code http://wordpress.pastebin.com/X1YSEKAC
some of the solution is shown in the wordpress forum.

salsa | June 27, 2010

Oh, You’re a God! No wonder your logo is Buddha. btw, your code http://wordpress.pastebin.com/4PRYEbF6 said something broken on line 58. I tried to find what happen but I couldnt find it. So as you said, it’s just CSS issue and I fix it with your previous code but I am curious with Code you just gave me. I would like to try it if you know which line is broken

btw, I still cannot change it to 4 columns instead of 3. I still cannot figure it out why, but this is lot better. anyway, this is my web screenshot http://yfrog.com/cb46901083j i dont know what html code of my browser so I can just only give you screenshot. And once again, I love you :* thank you sooooooo much

alchymyth | June 27, 2010

Hi Salsa,

here is a different code ( http://wordpress.pastebin.com/4PRYEbF6 ) which does the same as the other, just different and more simple.
you could use it instead of the code you pasted first.

i tried it in my local test blog, in category.php, and it seems to work fine.

i can’t say, why the other code did not work as expected.

hope this helped,
good luck,
michael

salsa | June 27, 2010

oh and the css is col1, .col2, .col3, .col4 {float:left; width:150px; padding-top:10px; padding-right:5px; } :)

salsa | June 27, 2010

hi alchymyth. Thank you very much for your reply. i never thought you reply this thou. I have post the code to http://wordpress.pastebin.com/Ec8pQTRS I do offline so i cannot show you my URL as i haven’t upload it. Those code should work perfectly to call category but as I said above it shows to
_______________________________title & thumbnail_______________________
_______________________________title & thumbnail_______________________
__title & thumbnail_ __title & thumbnail_
__title & thumbnail_ __title & thumbnail_

I tried http://wordpress.pastebin.com/CLYY2Y65 but it just shows all the posts I have (I want it to show only if somebody click the category) and everytime I change the coloumn into 4 coloumn it still shows 3 coloumn. Oh thank you so much for your reply. I also try to ask ppl on the forum.
Thanks in advance, regards,
Salsa

alchymyth | June 26, 2010

Hi Salsa,
difficult to tell without a link to your site and the code of your page template.
i suggest you take your question to the wordpress help forum at http://wordpress.org/support/ (you need to register there) and post a link and maybe the code. there are lots of experienced volunteer helpers, and i am usually there as well, and will try to help you when i see your question coming up.
it is much easier to exchange code and discuss these things in the help forum.

also, have a look at my other approach to grid posts: http://www.transformationpowertools.com/wordpress/playing-with-columns-stacking-posts-grid-style
it does pretty much the same as this article, particularly if you can format all posts with the same height.

all the best, alchymyth

salsa | June 26, 2010

hi Alchmyth, I have question, how do you put code to the explanation on your code. I have error, my post float like this :
_________________
_________________
__ __
__ __

what did i do wrong???

thanks for the answer

salsa | May 18, 2010

oh thanks so much for this!!!! I bookmark your web and i will try it as soon as possible. if it does work i will donate you some money soon after I have my paypal set up. if dont, please be ready for my questions :)

Brian | April 16, 2010

You rock! Alchmyth, it all works like a charm. really appreciates the time and effort.

brian

alchymyth | April 15, 2010

hi brian,
thank you for your interest.
i have uploaded a page template version of the code ( http://wordpress.pastebin.com/CLYY2Y65 ).
obviously, you need to adapt the classes of the div to suit your styles, and add whatever you want to display for each post.
the only main css class is ‘.col’ which should be set to ‘float:left; margin-right:5px; width:100px;’ with the ‘margin’ and ‘width’ set to a value so that the chosen number of columns fit the available space.

hope you can work with the code,
all the best,
alchymyth

ps: don’t forget that there is a wordpress support forum at http://wordpress.org/support/

Brian | April 15, 2010

Hi Alchymth,

Im trying to use your code for a page template unfortunately. Can you please translate your code for that usage please..

cheers
brian

alchymyth | April 7, 2010

hi joao,

thank you for your interest.
i would probably use the code in index.php to display the front page.
however, if you want to use it for a page template ( http://codex.wordpress.org/Pages#Page_Templates ), there may be a few changes neccesary to get it to show the posts.
if you need more assistence, sign up to the wordpress help forum ( http://wordpress.org/support/ ).

see you there,
alchymyth

Joao | April 7, 2010

I am now for days looking for a tutorial how to make multicolom posts.
You really break it dowd. Could you tell me where in wich files to put this code?

kind reagards

Joao

RSS feed for comments on this post. TrackBack URI

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Next article:
Previous article: