YogeshChauhan.com

WordPress: How to access first and random row values from a repeater field in ACF?

March 2, 2021

We saw a few different examples to get values from ACF like How to get ACF values from custom post type?, How to loop through a repeater field in ACF? and How to get ACF field values from another post?

Let’s continue to explore the wonderful ACF more…

The Repeater field is a really good solution for repeating content like slides, team members, directory, and many more repeating contents.

The Repeater field type acts as a parent to a set of sub fields which can be repeated again and again and there are no limits to the number of repeats either as long as you don’t specify.

The Repeater field will return an array of rows, where each row is an array containing sub field values.

When you add a repeater field, it creates additional functions specifically for looping over rows and accessing sub field values like have_rows, the_row, get_sub_field, and the_sub_field functions.

How to access the first row values from a repeater field?

Let’s see how to load a sub field value from the first row of a Repeater field.


$rows = get_field('repeater_field_name' );
if( $rows ) {
    $first_row = $rows[0];
    $first_row_title = $first_row['title'];
    // Do something...
}

How to access first row values from a repeater field using the break statement?

We can also use the break statement within a have_rows() loop to step out at any time.


if( have_rows('repeater_field_name') ) {
    while( have_rows('repeater_field_name') ) {
        the_row();
        $first_row_title = get_sub_field('title');
        // Do something...
        break;
    }
}

How to access the random row values from a repeater field?


$rows = get_field('repeater_field_name' );
if( $rows ) {
    $index = array_rand( $rows );
    $rand_row = $rows[ $index ];
    $rand_row_title = $rand_row['title'];
    // Do something...
}

dreamhost

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to uninstall Cocoapods from the Mac OS? #4 How to Use SQL MAX() Function with Dates? #5 How to add Read More Read Less Button using JavaScript? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

Apr 7 Solution for “Yarn build: Failed because of a stylelint error” Apr 7 Make sure your links have descriptive text Apr 7 How to add Laravel to WordPress using Sage theme (and install Tailwind CSS)? Apr 2 How to create a sidebar using pure CSS? Apr 1 How to switch dark and light themes using pure CSS? Apr 1 How to calculate elapsed time in JavaScript?

You might also like these

Solution to “Call to undefined function mysql_error()” in RevSlider WordPress PluginWordPressOOP, Class and Objects Strategies For Beginners (PHP)PHPWordPress: How to find all posts with a specific custom field value?WordPressLEFT JOIN in PostgresPostgresHow to center an image horizontally and vertically?CSSHow to create a sticky menu pin using CSS?CSS