我有以下代码:
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="item">
<?php the_post_thumbnail('full');?>
<div class="container">
<div class="carousel-caption">
<h1>
<?php the_title(); ?>
</h1>
<p>
<?php the_excerpt(); ?>
</p>
</div>
</div>
</div>
<?php endwhile; ?>
我需要将“active”类添加到第一个div(“item”旁边)
答案 0 :(得分:4)
使用布尔变量进行测试,在第一次传递后将其设置为true,以便进一步的循环不会将其标记为活动
<?php $firstMarked = false; ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="item <?php echo !$firstMarked ? "active":"";?>">
<?php the_post_thumbnail('full');?>
<div class="container">
<div class="carousel-caption">
<h1>
<?php the_title(); ?>
</h1>
<p>
<?php the_excerpt(); ?>
</p>
</div>
</div>
</div>
<?php $firstMarked = true;?>
<?php endwhile; ?>
答案 1 :(得分:2)
添加标志:
<?php
$isFrist = true;
while ( $loop->have_posts() ) : $loop->the_post();
?>
<div class="item">
<?php the_post_thumbnail('full');?>
<div class="container<?php if ($isFirst): ?> active<?php endif ?>">
<div class="carousel-caption">
<h1>
<?php the_title(); ?>
</h1>
<p>
<?php the_excerpt(); ?>
</p>
</div>
</div>
</div>
<?php
$isFrist = false;
endwhile;
?>
答案 2 :(得分:0)
if($loop->current_post == 1){
echo 'class';
}
答案 3 :(得分:0)
<script type="text/javascript">
$('.carousel-inner > :first-child').addClass("active");
</script>