-
-
Save creativerty/8031350 to your computer and use it in GitHub Desktop.
Gets random rows from ACF repeater fields with a count if required
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // Get the repeater field | |
| $repeater = get_field( 'repeater_field_name' ); | |
| // Count number of items | |
| $total = count($repeater); | |
| // Get a random rows. Change the second parameter in array_rand() to how many rows you want or keep as $total for all. | |
| $random_rows = array_rand( $repeater, $total ); | |
| // Loop through the random rows if more than one is returned | |
| if( is_array( $random_rows ) ){ | |
| foreach( $random_rows as $random_row ){ | |
| // Output data here. Replace sub field names. | |
| echo 'Sub Field 1: ' . $repeater[$random_row]['sub_field_1'] . '<br/>'; | |
| echo 'Sub Field 2: ' . $repeater[$random_row]['sub_field_2'] . '<br/><br/>'; | |
| } | |
| } else { | |
| // Output data here. Replace sub field names. | |
| echo 'Sub Field 1: ' . $repeater[$random_rows]['sub_field_1'] . '<br/>'; | |
| echo 'Sub Field 2: ' . $repeater[$random_rows]['sub_field_2'] . '<br/><br/>'; | |
| } | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment