Support

Account

Home Forums Add-ons Repeater Field Random 3 Rows of repeater field Reply To: Random 3 Rows of repeater field

  • Hi @danielvierab

    The following code should help you achieve that

    <?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, 3 );
        // 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/>';
        }
    ?>

    Let me know if this helps.