我有以下代码:
<?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”旁边)
最佳答案
使用 bool 变量进行测试,在第一次通过后将其设置为 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; ?>
关于php - 将不同的类添加到循环中的第一个 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18969038/