Skip to content

Instantly share code, notes, and snippets.

@zzap
Forked from wesrice/random-repeater.php
Last active August 29, 2015 14:19
Show Gist options
  • Save zzap/cc81af7352c16d976ad4 to your computer and use it in GitHub Desktop.
Save zzap/cc81af7352c16d976ad4 to your computer and use it in GitHub Desktop.
<?php
// Get the repeater field
$repeater = get_field( 'repeater_field_name' );
// Get a random rows. Change the second parameter in array_rand() to how many rows you want.
$random_rows = array_rand( $repeater, 2 );
// 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/>';
}
?>
<?php // If you only want one random item from ACF repeater, you can also do:
$repeater = get_field( 'repeater_field_name' );
$rand = rand(0, (count($repeater) - 1));
echo $repeater[$rand]['sub_field_1'];
// http://www.advancedcustomfields.com/resources/repeater/
// http://support.advancedcustomfields.com/forums/topic/random-repeater/#post-4506
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment