cre8d design - clean, clear, creative websites
  • Latest
  • Highlights
  • Discussions
  • Blogging
  • Design
  • Ebooks / Books
  • Social Media
  • Personal
  • About
  • Portfolio
  • Services
  • Blog
  • Contact
  • Twitter
  • Facebook
  • RSS

Organising posts into side-by-side columns in WordPress

Posted by Stephen Merriman on July 9, 2009

I have received many comments asking how to extend the earlier tutorial on How to organise posts into two side-by-side columns in WordPress to work with more than just two columns.

This can be done in a similar way by just editing two main files: index.php and style.css.

In index.php:

Before the WordPress Loop starts:
<?php $col = 0; $numcols = 3; ?>
Change numcols to another number if you want something other than 3 columns.

Just after the WordPress Loop:
<?php if ($ct && $ct%$numcols==0) echo '<div class="clearcol"></div>'; ?>
<div class="col">

Just before the end of the WordPress Loop:
</div>
<?php $ct++; ?>

Just after the end of the WordPress Loop:
<div class="clearcol"></div>

The exact code you need to add to style.css depends on how many columns you want, how much space you want between them, and how much width your theme allows for the posts.

For example, with the current WordPress Default theme, the width of the left column is 450px wide. If you wanted three columns, that’s 150px per column. If you also wanted 10px of spacing to the left and right of each column, that leaves a width of 130px for the post itself.

You would then need this code in style.css:
.clearcol {clear:both;}
.col {float:left;display:inline;width:130px;padding:0 10px;}

You’ll probably need to play around with these values until you get something that works.

The result for a 3-column WordPress Default theme can be seen here.

Facebook Statistics Application: What I'd like to see
Lifestreaming: a physical yearbook application
  • Pingback: Web Media Daily – July 9, 2009 | Reinventing Yourself...

  • http://www.teethremoval.com wisdom teeth removal

    Anyway to have columns in wordpress but only for specific pages. This seems to affect every page and post created.

  • http://rowl.es/portfolio/ Dan

    Thanks for a great bit of script firstly! I’m using it on my Portfolio page, I’m trying to use it twice on the same page for ‘Web Design’ and then for ‘Photography’, but it doesn’t seem to like it much on the second instance.

    http://rowl.es/portfolio/

    Thanks for any help!

  • http://rowl.es/portfolio/ Dan

    Its Ok I sorted it a colleague of mine figured if I put;

    $ct = 0;

    inside the first line of the second instance that would sort it, and it has!

    Thanks again.
    Dan

  • http://www.ronaldpoi.net/ Ronald Poi

    You just solved my entire week problem. Thank you very very much!

  • Matt

    Thanks this has been really useful.

  • Diem

    Thanks so much, been looking all over for this…

    Question though, I’ve been trying to do this 3 column for my archive section, so right now it works, but was wondering if there was a way to scale my posts just in the archive section so that it fits. Right now as it stands, it is just taking my posts as it appears in the index and placing it into 3 columns.

  • http://Table useruz

    I want to use this method in order to divide posts in table. And I changed the code like this:

    <?php if ($c && $c%$numcols==0) echo '’; ?>

  • http://Table useruz

    I want to use this method in order to divide posts in table. And I changed the code like this:

    <?php if ($c && $c%$numcols==0) echo '’; ?>

    Is there any mistake in my code?

  • http://www.iphoneguiden.se Olle Eriksson

    Thanks! Just what I needed.

  • Dagan

    Just a question, is it possible to do this with search results? Same Idea and concept, except for each search result to display in columns exactly this way?

  • http://www.darcyvoutt.com Darcy Voutt

    I am working on a website for a client and used this method. Unfortunately, the Next and Previous post buttons are not working.

    Is there something I should be doing that is special to this case?

    I am basically building a directory of posts.

    A quick reply would be very helpful!

    Cheers,

  • Stephen

    Dagan – yes, you can use this for search. If your theme has a search.php file, you can edit it in exactly the same way as mentioned. If not, you could create this file and copy the index.php file into it. See http://codex.wordpress.org/Template_Hierarchy for details.

    Darcy – this simply affects the layout on the page, and doesn’t alter how the previous/next links work at all. You might be doing something else wrong – I’d need to see your code to tell.

  • http://onlinegamesbestguide.co.cc/ Online Games Guide

    Thx for informations !! 5 Stars!

  • http://meegoblog.net Michael Cromarty

    Really useful thanks.

  • http://www.facebook.com/chandni.chauhan.716 Chandni Chauhan

    superlike….:)

  • DEX

    Thank you sooooo much!!

  • http://www.facebook.com/kathryn.born Kathryn Born

    Hi, so you say “just after” the loop, “just before the end” — but if my loop is like this (below)… what is “just before the end” – does that mean between the words “”php” and “endwhile”? Is it between the “)” and the “?”

    I don’t know PHP, and just want to cut and paste and get three columns…

    k

    • Stephen

      Hi Kathryn,

      It doesn’t look like your code ‘below’ came through.

      • http://www.facebook.com/kathryn.born Kathryn Born

        Sorry about that!

        (this is a theme called “blank slate” for people learning CSS and wordpress.

Hi, I'm Rachel Cunliffe!

Looking for a blog designer, Wordpress expert, website designer, or want to find out how I can help you? I'd love to hear from you.

Email:
rachel@cre8d-design.com
Phone:
(US) 646 233 3046
(NZ) 027 3833 746
Skype:
rachelcunliffe

Keep updated

Get blog posts via email

Follow @cre8d