simon-g
3/14/2015 - 2:54 AM

ACF Repeater - Grab First/LAst or Random Single Data Row

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

//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;

?>
<?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

//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;

?>