WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In the index.php template I want to display posts in 3columns in a table format. How can I do this? I don't want a list format cause I have variable hight image sizes

share|improve this question
up vote 1 down vote accepted

You still can use lists - just use proper CSS (every third li - first li in every row - should have clear property set; You can use CSS classes or :nth-child selector to do this).

If you really have to use table (I don't suggest it - it's not a very good idea, because it's not semantic), you can do something like this:

<?php while ( have_post() ): the_post(); ?>
  <php if ( $wp_query->current_post % 3 == 0 ): ?>
     <?php if ( $wp_query->current_post ): ?>
     <?php endif; ?> 
  <?php endif; ?>
<?php endwhile; ?>
<?php $i = $wp_query->current_post; while ( $i % 3 != 0 ): $i++ ?><td></td><?php endwhile; ?>

Not tested it, so it can be a little bit buggy, but idea should be clear.

share|improve this answer

Basically this can be done with a new WP_Query: http://codex.wordpress.org/Class_Reference/WP_Query

    // args
    $args = array(
        'post_type' => 'post'
    // new WP_Query
    $items_query = new WP_Query($args);        
    // loop
    if ($items_query->have_posts()) {
        while ($items_query->have_posts()) {
            $content_items .= '<h2>' . get_the_title() . '</h2>';
            $content_items .= get_the_content();
    // echo 
    echo $content_items;
    // reset

You have to add the markup for your table inside the loop. You should give some more information about the direction you want your posts to appear to create a complete snippet. For example:

     - 1st post - 2nd post - 3rd post -
     - 4th post - 5th post - 6th post -
     -   ....   -   ....   -   ...    -


     - 1st post - 3rd post - 5th post -
     - 2nd post - 4th post - 6th post -
     -   ....   -   ....   -   ...    -
share|improve this answer
I don't think you should create your own custom WP_Query to show recent posts on index template. Recent posts are already selected in main $wp_query, so there is no need to select them again... – Krzysiek Dróżdż Jun 28 '13 at 10:59
That's true, I only had "posts in 3columns in a table format" in mind, for recent posts in index.php you should use the mainquery. – rfrq Jun 28 '13 at 11:59

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.