如何计算ACF中继器输出中的总行数 [英] How to count the total number of rows in a ACF repeater output
本文介绍了如何计算ACF中继器输出中的总行数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问题:您如何简单地计算ACF转发器字段的输出中的行?
Question: How do you simply count the rows in the output of an ACF repeater field?
目标:在只有一行而不是多于一行的情况下,使用css类使输出看起来有所不同。
Goal: to make the output look different with a css class when there's only one row, vs. more than one row.
我的代码:
if( have_rows('testimonials')) {
$counter = 0;
$numtestimonials = '';
//loop thru the rows
while ( have_rows('testimonials') ){
the_row();
$counter++;
if ($counter < 2) {
$numtestimonials = 'onlyone';
}
echo '<div class="testimonial ' . $numtestimonials . '">';
// bunch of output here
echo '</div>';
}
}
很显然,我在这里的操作方式不起作用因为计数是< 2第一次穿过该行,因此即使以后再计算更多行也返回true。
Obviously, The way I do it here will not work as the count is < 2 the first time thru the row, so it returns true even if more rows are counted after.
谢谢!
推荐答案
好,我终于找到了答案。
OK, I finally found the answer to this.
在ACF转发器中对总行进行计数的方法是:
The way to count total rows in an ACF repeater is:
$numrows = count( get_sub_field( 'field_name' ) );
这篇关于如何计算ACF中继器输出中的总行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文