我有以下循环来创建我的轮播,但是我只需要在第一次迭代时添加活动类:
<?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;
?>
答案 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;
?>