如何计算 ACF 中继器输出中的总行数

How to count the total number of rows in a ACF repeater output

问题:如何简单地计算 ACF 转发器字段输出中的行数?

目标: 当只有一行而不是多行时,使用 css class 使输出看起来不同。

我的代码:

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,因此即使之后计算了更多行,它 returns 也是正确的。

谢谢!

您似乎想要重置 $numtestimonials 中的值。

因此,实际上,修复后的代码将是:

$output = "";
$numtestimonials = "";

while ( have_rows('testimonials') ){
    the_row();
    $counter++;
    $output .= "<span>" .$some_data. "</span>"; // bunch of output;
}

if($counter < 2){
    $numtestimonials = "onlyone";
}
$output = "<div class='testimonail ".$numtestimonials." '>"
              .$output
         ."</div>";
echo $output;

您可以这样获取行数:

$count = get_post_meta(get_the_ID(), 'testimonials', true);

显然这使用 get_the_ID() 检索当前 post ID - 您可能需要修改它。

ACF 根据转发器字段名称将转发器计数值存储为 post 元 table.

中的 meta_key

ACF 使用计数检索正确的转发器子字段值,这些值存储为 meta_keys 的值,格式为 $repeaterFieldname . '_' . $index . '_' . $subfieldName.

希望这对您有所帮助...

好的,我终于找到了答案。

计算 ACF 转发器中总行数的方法是:

$numrows = count( get_sub_field( 'field_name' ) );

您可以试试这段代码,它工作正常:

<?php if( have_rows('repeater_name') ):
$my_fields = get_field_object('repeater_name');
$count = (count($my_fields));
echo $count;
endif;?>

你可能想试试这个..

<?php 
$row = get_field('repeater', $post->ID);
if($row < 1) {
 $rows = 0;
} else {
 $rows = count($row);
} ?>
<p>Number of Row is (<?php echo $rows ; ?>)</p>

高级自定义字段有一个内置函数来计算版本 5.3.4 中添加的行数。

官方文档:ACF | get_row_index()

您可以在循环中使用 get_row_index();

<?php if( have_rows('slides') ): ?>
    <?php while( have_rows('slides') ): the_row(); ?>
        <div class="accordion" id="accordion-<?php echo get_row_index(); ?>">
            <h3><?php the_sub_field('title'); ?></h3>
            <?php the_sub_field('text'); ?>
        </div>
    <?php endwhile; ?>
<?php endif; ?>

The index returned from this function begins at 1. This means a Repeater field with 3 rows of data will produce indexes of 1, 2 and 3.

ACF Repeater Field 使用数组来存储行 获取字段行数的最简单方法是

$count = sizeof(get_sub_field('field_name'));

这对我有用,计数必须放在 if(have_rows('repeater_field') :

之前

三元运算符避免中继器为空时出现警告错误

如果将计数放在“if(have_rows('repeater_field')) :”之后,计数 returns FALSE

$repeater_field = get_sub_field('repeater_field');
// OR if repeater isn't a sub_field
// $repeater_field = get_field('repeater_field');

// ternary operator to avoid warning errors if no result
$count =  $repeater_field ? count($repeater_field) : FALSE;

if(have_rows('repeater_field')) : // OR if($count) :

    echo 'Number of posts:' . $count . '<br>';

    while(have_rows('repeater_field')) : the_row();
        echo get_sub_field('field_name') . '<br>';
    endwhile;
endif;