简体   繁体   中英

Adding div class based on PHP variable from ACF repeater field?

I am looping through a repeater field in Advanced Custom Fields and displaying divs for each item in the repeater. If the index is equal to 0 , I want to add a special class to just that div. Is this possible? Here's what I've tried:

<?php if (have_rows('products')): $i = 0; ?>
<div class="product-container">
    <?php while (have_rows('products')) : the_row(); ?>
    <div class="product <?php if ($i == 0) { echo 'active-class'; } ?>"></div>
    <?php $i++; endwhile; ?>
</div>
<?php endif; ?>

Unfortunately this is not working.

Instead of doing the conditional within the class, I just did it on the outside and defined two different class tags based on the condition:

<?php if (have_rows('products')): $i = 0; ?>
<div class="product-container">
    <?php while (have_rows('products')) : the_row(); ?>
    <div <?php if ($i == 0) { ?>class="product active"<?php } else { ?>class="product"<?php } ?> ></div>
    <?php $i++; endwhile; ?>
</div>
<?php endif; ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM