0

Is there a way to output posts from a single loop in multiple containers? Ideally I'd like to have the posts title/meta/content etc alternate between first-container and second-container, but I can't figure out a way to do it without multiple loops.

 <?php
        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
        query_posts('posts_per_page=3&paged=' . $paged); 
        ?>


        <?php if (have_posts()) : while ( have_posts()) : the_post(); ?>

    <div <?php post_class('clearfix') ?> id="post-<?php the_ID(); ?>">



     <h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>


        <div class="first-container">

                <?php global $more; $more = 0; ?>

                <?php the_post_thumbnail(); ?>

                <?php the_content('Read on...'); ?>

             </div> <!-- end div first-container -->         

    </div> <!-- end div post/clearfix -->

FULL

<?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    query_posts('posts_per_page=3&paged=' . $paged); 
    ?>

    <?php $class = ''; 
    <?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(); ?>">



<div class="metawrapper">
<div class="leftmeta">

    <div class="date-house">
    <span><?php the_time('M jS, Y') ?></span>
    </div> <!-- end div date house -->


</div> <!-- end div leftmeta -->

<div class="meta">

        <?php echo '<em>Written by </em> <br>'; the_author_posts_link(); ?> 


         <?php the_tags('Tags: ', ', ', '<br />'); ?>


</div> <!-- end div meta -->


<div class="commentsbutton">

<span>Comments</span>

<?php comments_popup_link('No Comments', '1 Comment', '% Comments', 'comments-link', ''); ?>

</div> <!-- end div commentsbutton -->


</div> <!-- end div metawrapper -->





        <div class="entrywrapper">

            <div class="entry">


            <div class="bordertop">
             <h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
            </div> <!-- end div bordertop -->




            <?php global $more; $more = 0; ?>







            <?php the_post_thumbnail(); ?>





            <div class="spacersingle"></div> <!-- end div spacersingle -->

                <?php the_content('Read on...'); ?>


                <div class="postbreaker"></div> <!-- end div entrybreaker -->
            </div> <!-- end div entry -->

<div class="<?php echo $class ?>">






<div style="clear:both"></div>


        </div> <!-- end div entrywrapper -->
<div style="clear:both"></div>

        </div> <!-- end div post -->




<?php endwhile; endif; ?>

1 Answer 1

0
<?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="<?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.

5
  • Uh, I'm sorry but I have no idea what you mean, I'm not an expert in php and I can't see how to implement this code with what I already have.
    – andy
    Commented Dec 30, 2012 at 17:40
  • I copied part of your code, altered it, and pasted back a version that will do what you want. Match up the code I posted to the code you posted. In my code, I added the first and fourth lines, and altered the last line. hmmm... just replace everything in your code up to an including <div class="first-container">.
    – s_ha_dum
    Commented Dec 30, 2012 at 17:47
  • I edited the first post with my full code for the loop with yours included, it just gives me a server error.
    – andy
    Commented Dec 30, 2012 at 19:06
  • You open php on the line $class = ''; and then open it again on the next line without closing it between. The error you receieve should tell you the line number.
    – Milo
    Commented Dec 30, 2012 at 22:22
  • It is the first line of my code. I broke it.
    – s_ha_dum
    Commented Dec 30, 2012 at 22:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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