Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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
add comment

2 Answers

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:

<table>
<?php while ( have_post() ): the_post(); ?>
  <php if ( $wp_query->current_post % 3 == 0 ): ?>
     <?php if ( $wp_query->current_post ): ?>
     </tr>
     <?php endif; ?> 
     <tr>
  <?php endif; ?>
  <td>
     ... YOUR POST GOES HERE
  </td>
<?php endwhile; ?>
<?php $i = $wp_query->current_post; while ( $i % 3 != 0 ): $i++ ?><td></td><?php endwhile; ?>
  </tr>
</table>

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

share|improve this answer
add comment

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()) {
            $items_query->the_post();
            $content_items .= '<h2>' . get_the_title() . '</h2>';
            $content_items .= get_the_content();
        }
    }
    // echo 
    echo $content_items;
    // reset
    wp_reset_postdata();

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

or

     ----------------------------------
     - 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
add comment

Your Answer

 
discard

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.