0

This question already has an answer here:

I need to separate WordPress posts loop in to 2 columns , left ( all odds ) and right ( all evens ) .

Currently I am doing this with 2 separate loops but would like to make it with one and would like this to be done by the WP coding standards as much as possible.

<div class="left-side">
    <?php 
        $row_start = 1; 
        while ( have_posts() ) : the_post(); 
            if( $row_start % 2  != 0) {// odd
                get_template_part( 'blog','item');
            }
            ++$row_start; 
        endwhile;
    ?>
</div>
<div class="right-side">
    <?php  
      $row_start = 1; 
       while ( have_posts() ) : the_post(); 
        if( $row_start % 2 == 0) {// even
            get_template_part( 'blog','item');
        }
        ++$row_start; 
        endwhile;
    ?>
</div>

Any help is appreciated!

marked as duplicate by fuxia Apr 1 '16 at 0:29

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • So you want post 1 on left, post 2 on right, post 3 on left and so on? If so this can be accomplished via CSS. A List Apart has a good article on multi column layouts alistapart.com/article/multicolumnlayouts – czerspalace Mar 31 '16 at 22:04
  • @czerspalace dealing with 2 column timeline layout and need to separate all odd left and all even right , not just do floats or masonry. Actually trying to avoid those and do pure css , thus the separation in to two columns only. – Benn Mar 31 '16 at 22:12
1
   <?php  
     $row_start = 1; 
     while ( have_posts() ) : the_post(); 
        if( $row_start % 2  != 0) {// odd
        ?>
          <div class="left-side"> 
             <?php get_template_part( 'blog','item'); ?> 
          </div> 
        <?php
        }
        if( $row_start % 2 == 0) {// even
        ?> 
          <div class="right-side"> 
             <?php get_template_part( 'blog','item');?> 
          </div> 
        <?php
        }
        ++$row_start; 
     endwhile;
    ?>

You could always break in and out of PHP to achieve what you're after. I haven't tested the above, but something similar should work.

Or ...

You could avoid breaking in and out of PHP by echoing the HTML:

<?php  
     $row_start = 1; 
     while ( have_posts() ) : the_post(); 
        if( $row_start % 2  != 0) {// odd
          echo '<div class="left-side">';
          get_template_part( 'blog','item');
          echo '</div>'; 
        }
        if( $row_start % 2 == 0) {// even
          echo '<div class="right-side">';
          get_template_part( 'blog','item');
          echo '</div>';
        }
        ++$row_start; 
     endwhile;
    ?>

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