简体   繁体   English

从ACF转发器字段添加基于PHP变量的div类?

[英]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. 我在“高级自定义字段”中遍历一个转发器字段,并显示该转发器中每个项目的div。 If the index is equal to 0 , I want to add a special class to just that div. 如果索引等于0 ,我想向该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; ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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