在循环的第一次迭代中添加一个类

时间:2014-05-21 15:15:26

标签: php twitter-bootstrap loops carousel

我有以下循环来创建我的轮播,但是我只需要在第一次迭代时添加活动类:

<?php
$args = array(
'post_type' => 'homepage_banner',
'orderby'=>'menu_order','order'=>'ASC');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();

echo '<div class="item active"> <img alt="';
echo the_title();
echo'" data-src="" src="">
  <div class="container">
    <div class="carousel-caption"><h2>';
    echo the_title();
      echo '</h2><p>';
      echo the_content();
      echo'</p>
    </div>
  </div>
</div>';

endwhile;

?>

1 个答案:

答案 0 :(得分:1)

你可以这样做:

<?php

 $args = array(
     'post_type' => 'homepage_banner',
     'orderby'=>'menu_order','order'=>'ASC');
 $loop = new WP_Query( $args );
 $active = ' active'; //<============== updated line 1
 while ( $loop->have_posts() ) : $loop->the_post();

     echo '<div class="item'. $active .'"> <img alt="';

     echo the_title();
     echo'" data-src="" src="">
     <div class="container">
        <div class="carousel-caption"><h2>';
             echo the_title();
             echo '</h2><p>';
             echo the_content();
             echo'</p>
         </div>
     </div>
     </div>';
     $active = ''; //<============== updated line 2

 endwhile;

?>