【发布时间】:2014-12-31 18:22:28
【问题描述】:
我有一个中继器,我想根据它输出的字段数来改变它
我的基本转发器代码:
<?php // REAPEATER FIELD
if(get_field('whatever')): ?>
<?php while(has_sub_field('whatever')): ?>
<div class="whatever_block">
<?php the_sub_field('anything'); ?>
</div>
<?php endwhile; ?>
<?php endif; ?>
我希望它看起来如何:
<?php // REAPEATER FIELD
if(get_field('whatever')): ?>
<?php while(has_sub_field('whatever')): ?>
//if only one
<div class="col-sm-12">
<?php the_sub_field('anything'); ?>
</div>
//if only two
<div class="col-sm-6">
<?php the_sub_field('anything'); ?>
</div>
//if etc
//else
<div class="whatever_block">
<?php the_sub_field('anything'); ?>
</div>
<?php endwhile; ?>
<?php endif; ?>
如何使用 if 语句实现上述输出? if 语句甚至是正确的方法吗?
【问题讨论】:
-
你有什么问题?
-
@Celeo ,当您发表评论时,我只是在更新我的问题。我正在尝试实现第二个版本(我希望它看起来如何)。所以我在想一个 if 语句,我会根据有多少项目来改变我输出的类。这更有意义吗?
-
一次最多可以激活多少个字段?就像在 ACF 中一样,您是否设置了 Max 数?
-
@Joe,是的,我将最大数量设置为 4
标签: php wordpress if-statement advanced-custom-fields