Home › Forums › Add-ons › Repeater Field › How to count the number of repeater fields within a flexible layout?
I’m trying to get a count for the number of columns in my multi-column_row
so I can use the number of columns when building my layout. I feel like i’ve tried every example I could find but I still can’t seem to get the number of rows BEFORE doing my while loop.
Here’s my ACF Loop:
<?php if ( have_rows( 'content' ) ): ?>
<?php while ( have_rows( 'content' ) ) : the_row(); ?>
<?php if ( get_row_layout() == 'text_on_white' ) : ?>
<?php the_sub_field( 'anchor_link' ); ?>
<?php the_sub_field( 'text' ); ?>
<?php elseif ( get_row_layout() == 'centered_video' ) : ?>
<?php the_sub_field( 'anchor_link' ); ?>
<?php $thumbnail_image = get_sub_field( 'thumbnail_image' ); ?>
<?php if ( $thumbnail_image ) : ?>
" alt="<?php echo esc_attr( $thumbnail_image['alt'] ); ?>" />
<?php endif; ?>
<?php the_sub_field( 'video_url' ); ?>
<?php elseif ( get_row_layout() == 'multi-column_row' ) : ?>
<?php the_sub_field( 'anchor_link' ); ?>
<?php if ( have_rows( 'columns' ) ) : ?>
<?php while ( have_rows( 'columns' ) ) : the_row(); ?>
<?php $image = get_sub_field( 'image' ); ?>
<?php if ( $image ) : ?>
" alt="<?php echo esc_attr( $image['alt'] ); ?>" />
<?php endif; ?>
<?php the_sub_field( 'wysiwyg' ); ?>
<?php endwhile; ?>
<?php else : ?>
<?php // No rows found ?>
<?php endif; ?>
<?php elseif ( get_row_layout() == 'divider_line' ) : ?>
<?php // Warning: Layout 'divider_line' has no sub fields ?>
<?php endif; ?>
<?php endwhile; ?>
<?php else: ?>
<?php // No layouts found ?>
<?php endif; ?>
I’ve tried
$count = count(get_field('grid_block'));
<?php
$cards = get_sub_field('card');
if(have_rows('card')):
$number_of_cards = count($cards);
endif; ?>
and neither are working. Any tips?
I don’t see how the second parts of your code fit into the first.
To get the count of rows it is either
$count = count(get_field('repeater_field_name'));
OR
$count = count(get_field('repeater_field_name'));
depending, of course if it is a sub field.
In any case you must get the count of rows before calling have_rows() for the repeater you want the count of.
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.