2017-01-30 46 views
1

我有兩個環路與排序依據隨機的(所有的工作好),但我想說明每個循環同一職位, 例子:我有交1至10; 循環1顯示帖子1 2和3; 環2需要顯示同一職位1 2和3,我的代碼是:2循環隨機和同一職位的WordPress

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

不要使用兩個查詢 - 使用一個,並循環兩次結果。 https://codex.wordpress.org/Function_Reference/rewind_posts – CBroe

回答

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

感謝所有現在的工作100%。 – EzraMod