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 Reply To: Loop For Even/Odd Posts

#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; ?>