Skip to content

Instantly share code, notes, and snippets.

@sumonst21
Forked from neilgee/acf-first-row.php
Created September 23, 2019 22:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sumonst21/9b14db53323302b0f6aa6721a01b80b7 to your computer and use it in GitHub Desktop.
Save sumonst21/9b14db53323302b0f6aa6721a01b80b7 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