I have this loop for wordpress which makes the posts display in 2 columns, however I need it to display across 3 columns and have tried a number of ways but nothing seems to work, does anyone have any idea how I could do this?

<?php if (have_posts()) : ?>

     <?php
   $posts_this_page = sizeof($wp_query->posts);
   $posts_per_col = ceil($posts_this_page/2);
   $counter = 0; ?>
   <div class='leftcol' >

        <?php while (have_posts()) : the_post(); ?>
         <?php ++$counter;
      if ($counter == ($posts_per_col + 1)) {
         echo '</div><!-- End leftcol --><div class="rightcol">';
      } ?>
        <div class="polar">
         <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>


                <?php if(is_home()) { if ( function_exists('wp_list_comments') ) { ?> <div <?php post_class(); ?>> <?php }} ?>




                <div class="entry">
                    <?php the_excerpt('&raquo; Read the rest of this entry &laquo;'); ?>
                    <?php wp_link_pages(array('before' => '<p>Page: ', 'after' => '</p>', 'next_or_number' => 'number')); ?>


                </div>
                <?php if(is_home()) { if ( function_exists('wp_list_comments') ) { ?></div><!-- close post_class --><?php }} ?>
      </div>
      <?php endwhile; ?>
   </div><!-- End left/rightcol -->
  • It's not really clear what it is that doesn't work. Are you having a formatting problem, or is your PHP failing to pull the content you expect it to? Nathan Duran about 9 years ago
  • The php in that version works fine but only pulls posts into 2 columns, the one below works much better richard bruce about 9 years ago

1 answer

0
points
This was chosen as the best answer

ok managed to solve this myself and thought i'd post the answer incase its helpful to anyone:

<?php $count = 1;
$col_count = 3;
$num_of_posts = $wp_query->post_count;
$post_per_column = ceil($num_of_posts / $col_count); ?>
<div id="col1">
<?php while (have_posts()) : the_post(); ?>

  <div class="post" id="post-<?php the_ID(); ?>">
    <h1><a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h1>
    <div class="descr"><?php the_time('l, F jS Y, G:i a T'); ?></div>
    <div class="entry">
      <?php the_content('Read the rest of this entry &raquo;'); ?>
    </div>
  </div>

  <?php if($count == $post_per_column) { echo '</div><div id="col2">'; }
  if($count == 2*$post_per_column) { echo '</div><div id="col3">'; }
  $count++;
  endwhile;
?>
</div><!--end #col3 -->

and here's the css:

#col1, #col2, #col3 { float:left; width:30%; margin-right:10px;  }
#col1 { clear:left; }
#col3 {margin-right: 0px; }
Answered about 9 years ago by richard bruce