Skip to content

Instantly share code, notes, and snippets.

@neilgee
Last active September 17, 2021 18:20
  • Star 9 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save neilgee/7216070bd952e8fe7170 to your computer and use it in GitHub Desktop.
ACF Repeater - Grab First/LAst or Random Single Data Row
<?php
//My ACF Fields for reference
//testimonials - field group
//testimonial - sub-field
//testimonial_header - sub-field
//First Repeater Row in Array
$rows = get_field( 'testimonials', 348 );// grab all rows from page ID
$testimonial_content = $rows[0]['testimonial'];//grabs first testimonial content- change number in both for different row
$testimonial_header = $rows[0]['testimonial_header'];//grabs first testimonial header
$first = '<blockquote>' . $testimonial_content . '</blockquote><p>' . $testimonial_header . '</p>'; //set surrounding HTML markup
echo $first;
?>
<?php
//My ACF Fields for reference
//testimonials - field group
//testimonial - sub-field
//testimonial_header - sub-field
//Last Row in Array
$rows = get_field( 'testimonials', 348 ); // get all the rows from and page ID
$end_row = end( $rows ); // get the end row
$testimonial_content = $end_row['testimonial' ]; // get the sub field value
$testimonial_content = $end_row['testimonial_header' ]; // get the sub field value
$last = '<blockquote>' . $testimonial_content . '</blockquote><p>' . $testimonial_content . '</p>'; //set surrounding HTML markup
echo $last;
?>
<?php
//My ACF Fields for reference
//testimonials - field group
//testimonial - sub-field
//testimonial_header - sub-field
//Random Single Repeater Row in Array
$rows = get_field( 'testimonials', 348 ); // grab all rows from page ID
$rand_row = $rows[ array_rand( $rows ) ]; // grab a random row
$testimonial_content = $rand_row['testimonial' ]; // get the sub field value
$testimonial_content = $rand_row['testimonial_header' ]; // get the sub field value
$random = '<blockquote>' . $testimonial_content . '</blockquote><p>' . $testimonial_header . '</p>'; //set surrounding HTML markup
echo $random;
?>
@carljbusch
Copy link

This worked great! Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment