Home › Forums › Add-ons › Repeater Field › If repeater field has 1 rows, 2 rows etc › Reply To: If repeater field has 1 rows, 2 rows etc
In your frontend, you’ll have to count number of elements in the repeater field array. So something like this:
<?php
// pre define the allowed columns
$allowed_classnames = array(
1 => 'full',
2 => 'half',
3 => 'third',
4 => 'fourth',
5 => 'fifth',
);
// get the count on the repeater field
// mabye use get_sub_field() instead of get_field() if it's nested
$number_of_cols = count( get_field( '<repeater>' ) );
// set a default classname
$classname_to_use = $allowed_classnames[1];
// check if the $number_of_cols exist in the predefined classnames
if ( array_key_exists( $number_of_cols , $allowed_classnames ) ) {
// set the classname to be used
$classname_to_use = $allowed_classnames[$number_of_cols];
}
while( has_sub_field( '<repeater>' ) ) : ?>
<div class="<?php echo esc_attr( $classname_to_use ); ?>">
<?php the_sub_field( '<repeater_content>' ); ?>
</div>
<?php endwhile; ?>
Please not that I just briefly tested it on a site I’m working on. You’ll have to replace the '<repeater_content>'
and '<repeater>'
with your actual field names.
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.