I’m looking to add a class ‘active’ to the first div item in a gallery. When I add the counter code it is adding the class to every div rather than just the first one.
Here is my code:
<?php
$images = get_field(‘product_page_images’);
if( $images ):
?>
<div class=”carousel-inner” role=”listbox”>
<?php $count = 0; foreach( $images as $image ):?>
<div class=”item <?php if ($count == 0) {echo ‘active’;}?>”>
” alt=”<?php echo $image[‘alt’]; ?>” />
</div>
<?php endforeach; ?>
</div>
<?php $count++; endif; ?>
Try this,
<?php
$images = get_field(‘product_page_images’);
if( $images ):
?>
<div class=”carousel-inner” role=”listbox”>
<?php $count = 0; foreach( $images as $image ):?>
<div class=”item <?php if (!$count): ?>active<?php endif; ?>”>
” alt=”<?php echo $image[‘alt’]; ?>” />
</div>
<?php $count++; endforeach; ?>
</div>
<?php endif; ?>