计算sub_field中有多少行 [英] Count how many rows are in sub_field

查看:82
本文介绍了计算sub_field中有多少行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用高级自定义字段中继器来加载一些sub_fields,您可以在下面的代码中看到这些内容:

I am using advanced custom field repeater to load some sub_fields which you can see in the below code:

<?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; ?>

我想计算多少 .row-item 存在,然后将其作为类打印在容器上#role-wrapper

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 .

因此,作为其外观的HTML演示:

So as a HTML demo of how it would look:

<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>


推荐答案

文档 get_field()返回一个数组()子字段。结果,您可以执行一个简单的 count()

As specified by the docs, get_field() returns an array() of sub fields in this case. As a result, you can do a simple count():

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

这篇关于计算sub_field中有多少行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆