Skip to content

Instantly share code, notes, and snippets.

@wesrice
Created February 27, 2012 16:02
Show Gist options
  • Select an option

  • Save wesrice/1924934 to your computer and use it in GitHub Desktop.

Select an option

Save wesrice/1924934 to your computer and use it in GitHub Desktop.

Revisions

  1. wesrice revised this gist Feb 28, 2012. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions random-repeater.php
    Original file line number Diff line number Diff line change
    @@ -22,6 +22,7 @@
    // 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/>';

    }

    ?>
  2. wesrice created this gist Feb 27, 2012.
    27 changes: 27 additions & 0 deletions random-repeater.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    <?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/>';
    }

    ?>