Skip to content

Instantly share code, notes, and snippets.

@jessedmatlock
Forked from neilgee/acf-first-row.php
Created September 17, 2021 18:20
Show Gist options
  • Save jessedmatlock/721db34ec94d32e2287b5c1bb2300e06 to your computer and use it in GitHub Desktop.
Save jessedmatlock/721db34ec94d32e2287b5c1bb2300e06 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;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment