Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Loop For Even/Odd Posts

  • This topic is empty.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #151309
    Steven Morgan
    Participant

    I have two columns and I want one post type to get distributed to each column evenly. So it’s two side-by-side divs and I want:

    Div1 = Post1, Post3, Post5 Div2 = Post2, Post4, Post6

    So basically get odd/even posts. Not exactly sure how to do it.

    <?php query_posts('post_type=post-type'); ?>
    <?php if (have_posts()) : ?>  
    <?php while (have_posts()) : the_post(); ?>  
    
    <div class="column1">
    <?php 
      //Get Odd Posts
    ?>
    </div>
    
    <div class="column2">
    <?php 
      //Get Even Posts
    ?>
    </div>
    
     <?php endwhile; ?>  
    <?php else : ?>  
    //Something that happens when a post isn’t found.  
    <?php endif; ?>
    
    #151333
    Alen
    Participant

    Try this (Untested)

    <?php if(have_posts()) : ?>
    <?php
    
        $i = 0;
        while(have_posts())
        {
            $key = $i & 1 ? 'odd' : 'even';
            $post[$key] = array(get_the_title() => get_the_content());
            $i++;
        }
    ?>
    
    <div class="even">
    <?php foreach ($post['even'] as $title => $content) : ?>
        <h2><?php echo $title; ?></h2>
        <?php echo $content; ?>
    <?php endforeach; ?>
    </div>
    <?php // END even ?>
    
    <div class="odd">
    <?php foreach ($post['odd'] as $title => $content) : ?>
        <h2><?php echo $title; ?></h2>
        <?php echo $content; ?>
    <?php endforeach; ?>
    </div>
    <?php // END odd ?>
    
    <?php else : ?>  
        <h2>No posts found</h2>  
    <?php endif; ?>
    
Viewing 2 posts - 1 through 2 (of 2 total)
  • The forum ‘Back End’ is closed to new topics and replies.