Post from one loop in different containers?

<?php $class=""; // set your class to nothing outside the loop ?>
<?php if (have_posts()) : while ( have_posts()) : the_post(); ?>

    <?php $class = ('first-container' == $class) ? 'second-container' : 'first-container'; // alternate classes ?>

    <div <?php post_class('clearfix') ?> id="post-<?php the_ID(); ?>">
     <h2><a href="https://wordpress.stackexchange.com/questions/77704/<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
        <div class="<?php echo $class ?>">

If $class equals “first-container” set it to “second-container” else set it to “first-container”. Because we set $container to nothing initially, the first iteration will set $class to “first-container”, after which you alternate.

Leave a Comment