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)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.