2 loop with random and same post WordPress

2019-07-23 20:11发布

I have two loop with orderby random(all work good), but I want to show same post for each loop, example: I have post 1 to 10; loop 1 show post 1 2 and 3; loop 2 need to show same post 1 2 and 3, my code is:

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

标签: php wordpress
1条回答
ら.Afraid
2楼-- · 2019-07-23 20:30

Try this, I think it will work for you.

<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>
查看更多
登录 后发表回答