最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Post from one loop in different containers?

programmeradmin1浏览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; ?>

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 edited Dec 30, 2012 at 19:06 andy asked Dec 30, 2012 at 15:52 andyandy 2,3598 gold badges25 silver badges34 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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.

发布评论

评论列表(0)

  1. 暂无评论