Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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; ?>
share|improve this question

1 Answer 1

<?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.

share|improve this answer
    
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 Dec 30 '12 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 Dec 30 '12 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 Dec 30 '12 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 Dec 30 '12 at 22:22
    
It is the first line of my code. I broke it. –  s_ha_dum Dec 30 '12 at 22:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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