简体   繁体   English

计算sub_field中有多少行

[英]Count how many rows are in sub_field

I am using advanced custom field repeater to load some sub_fields which you can see in the below code: 我正在使用高级自定义字段转发器来加载一些sub_fields,您可以在下面的代码中看到这些代码:

<?php
    if( get_field('who_made_it') ): ?>
    <div id="role-wrapper">
        <?php while( has_sub_field('who_made_it') ): ?>
            <div class="role-item">
                <?php the_sub_field('job_role'); ?>
                <?php the_sub_field('description'); ?>
            </div>
        <?php endwhile; ?>
    </div>
<?php endif; ?>

I would like to count how many .row-item 's there are and then print that number as a class on the container #role-wrapper . 我想算一下有多少.row-item ,然后将该数字作为一个类打印在#role-wrapper容器上。

So as a HTML demo of how it would look: 因此,作为其外观的HTML演示:

<div id="role-wrapper" class"roleitems-3">
    <div class="role-item">
        content in here
    </div>
    <div class="role-item">
        content in here
    </div>
    <div class="role-item">
        content in here
    </div>
</div>

As specified by the docs , get_field() returns an array() of sub fields in this case. docs所指定,在这种情况下, get_field()返回子字段的array() As a result, you can do a simple count() : 结果,您可以执行一个简单的count()

<div id="role-wrapper" class"roleitems-<?php echo count( get_field('who_made_it') ); ?>">

I am unfamiliar with has_sub_field and the advanced custom field repeater , but it seems a simple answer would be to add a counter. 我不熟悉has_sub_fieldadvanced custom field repeater ,但似乎一个简单的答案是添加一个计数器。

<?php 
$counter = 0;
while( has_sub_field('who_made_it') ):
    //do stuff
    $counter++;
endwhile;

//output the counter however you like
echo('<div class="counter">Total: ' . $counter . '</div>');
?>

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

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