2循环随机和相同的帖子WordPress

时间:2017-01-30 10:53:07

标签: php wordpress

我有两个循环with orderby random(一切正常),但我想为每个循环显示相同的帖子, 例如:我发布了1到10个; 循环1显示帖子1 2和3; 循环2需要显示相同的帖子1 2和3,我的代码是:

<div class="col-md-12" data-wow-delay="0.2s">
        <div class="carousel slide" data-ride="carousel" id="quote-carousel">
        <!-- Bottom Carousel Indicators -->
                <ol class="carousel-indicators">
<?php
$args = array( 
'post_type' => 'testimonials', 
'orderby' => 'rand', 
'posts_per_page' => 3 );
$loop = new WP_Query( $args );
$i = 0;
while ( $loop->have_posts() ) : $loop->the_post();

$avatar_testimonials = get_field('avatar-testimonials');
?>               
                    <li data-target="#quote-carousel" data-slide-to="<?php echo $i; ?>" class="<?php if ($i == 0) echo 'active'; ?>"><img class="img-responsive" alt="<?php echo the_title(); ?>" src="<?php if ($avatar_testimonials) {echo $avatar_testimonials['url'];} else {the_post_thumbnail_url('thumbnail');} ?>" alt=""></li>                
<?php $i++; endwhile;  ?>
                </ol>
                <!-- Carousel Slides / Quotes -->
                <div class="carousel-inner text-center">
<?php
$args = array( 
'post_type' => 'testimonials', 
'orderby' => 'rand', 
'posts_per_page' => 3 );
$loop = new WP_Query( $args );
$i = 0;
while ( $loop->have_posts() ) : $loop->the_post();
?>

                                <!-- Quote 1 -->
                    <div class="item <?php if ($i == 0) echo 'active'; ?>">
                        <blockquote>
                            <div class="row">
                                <div class="col-sm-8 col-sm-offset-2">
                                        <?php echo the_excerpt(); ?>
                                    <small><a href="<?php echo the_permalink(); ?>" title="<?php echo the_title(); ?>"><?php echo the_title(); ?></a></small>
                                </div>
                            </div>
                        </blockquote>
                    </div>
<?php $i++; endwhile;  wp_reset_postdata(); ?>
                </div>
        </div>
    </div>

1 个答案:

答案 0 :(得分:1)

试试这个,我认为它适合你。

<div class="col-md-12" data-wow-delay="0.2s">
    <div class="carousel slide" data-ride="carousel" id="quote-carousel">
        <!-- Bottom Carousel Indicators -->
        <ol class="carousel-indicators">
            <?php
            $args = array( 
            'post_type' => 'testimonials', 
            'orderby' => 'rand', 
            'posts_per_page' => 3 );

            $loop = new WP_Query( $args );
            $i = 0;
            while ( $loop->have_posts() ) : $loop->the_post();

            $avatar_testimonials = get_field('avatar-testimonials');
            ?>               
                <li data-target="#quote-carousel" data-slide-to="<?php echo $i; ?>" class="<?php if ($i == 0) echo 'active'; ?>"><img class="img-responsive" alt="<?php echo the_title(); ?>" src="<?php if ($avatar_testimonials) {echo $avatar_testimonials['url'];} else {the_post_thumbnail_url('thumbnail');} ?>" alt=""></li>                
            <?php $i++; endwhile;  ?>
        </ol>

        <!-- Carousel Slides / Quotes -->
        <div class="carousel-inner text-center">
            <?php

            $i = 0;
            while ( $loop->have_posts() ) : $loop->the_post();
            ?>

            <!-- Quote 1 -->
            <div class="item <?php if ($i == 0) echo 'active'; ?>">
                <blockquote>
                    <div class="row">
                        <div class="col-sm-8 col-sm-offset-2">
                                <?php echo the_excerpt(); ?>
                            <small><a href="<?php echo the_permalink(); ?>" title="<?php echo the_title(); ?>"><?php echo the_title(); ?></a></small>
                        </div>
                    </div>
                </blockquote>
            </div>
            <?php $i++; endwhile;  wp_reset_postdata(); ?>

        </div>
    </div>
</div>