Support

Account

Home Forums Add-ons Gallery Field Adding class to first gallery item

Solving

Adding class to first gallery item

  • 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; ?>
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Adding class to first gallery item’ is closed to new replies.