Support

Account

Home Forums Add-ons Repeater Field Display random rows in repeater while limiting the number shown

Helping

Display random rows in repeater while limiting the number shown

  • To preface this… I know how to display a random row from a repeater. I know how I can limit the number of rows that get displayed from a repeater. What I can’t figure out is how to do both at the same time. Displaying a random row will only display one. I’d like to be able to display multiple random rows. I’ve tried searching through these forums and there are plenty of examples of how to do both things separately, but no combination of anything seems to work for me.

    Ideas?

  • I’m assuming you mean on the front-end / display end and not the admin panel. If I’m mistaken please reply back and we can come up with a different solution. Additionally I don’t have Pro but from what I remember the repeater field is simply an array so instead of using the helper functions you can get the raw results using get_field().

    // Get our repeater values
    $repeater_arr = get_field( 'repeater-name-or-key', $post_id );
    
    /**
     * We're going to shuffle the repeater array ( randomize the whole thing )
     * Which is done by reference, so lets put it in a temporary value
     * So we don't mess with our original repeater_arr value
     */
    $temp_arr = $repeater_arr;
    shuffle( $temp_arr );
    
    // Grab up to 10 values of the array
    $random_slice = array_slice( $temp_arr, 0, 10 );
    
    // Loop through the random subfields
    foreach( $random_slice as $subfield ) {
    	echo $subfield['title'];
    }
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.