Outputting the First or Last Repeater Row in ACF to a Widget in WordPress

This guide follows on from a previous one using ACF Pro repeater field group to output rows of Testimonials on a WordPress page instead of using a custom post type single/archive structure.

What I wanted to also show is how you can take one of these testimonial data rows and display it in a widget on the home page that just pulls the first, last or random row from the actual testimonial page. Here I will be adding PHP code in widgets so you’ll  need to ensure that you are allowing PHP to run in widgets.

ACF Repeater First

Single Testimonial on Home Page

So in the layout above instead of manually copying and pasting a single testimonial into a widget, we can get a single ACF repeater data row in there or a random or latest one from the existing data.

 

acf-testimonial-styled-css

Existing Testimonial Page

Just add the relavant PHP code below into the required widget.

First Repeater 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;
?>
view raw acf-first-row.php hosted with ❤ by GitHub

Last Repeater Row

<?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;
?>
view raw acf-last-row.php hosted with ❤ by GitHub

Random Repeater 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;
?>
view raw acf-random-row.php hosted with ❤ by GitHub

I have marked up the code in the Gist – essentially in all three we are initially grabbing the custom field which is called testimonials and the page ID that it is on.

For the first row, 2 variables are set against each sub-field row with a value of [0] which is the first in an array, then a final variable is assigned $first and echo’ed out.

For the random row, again all rows are grabbed and then a random row is grabbed with array_rand, the sub-fields are then also assigned variables, then a final variable is assigned $random and echo’ed out.

For the last row, all rows are grabbed and then the end row is grabbed with end, the sub-fields are then also assigned variables, then a final variable is assigned $random and echo’ed out.

More ACF Repeater code is here, I think being able to extract specific rows of data and output them elsewhere really makes this tool like a Swiss Army Knife.

Leave a Comment





%d bloggers like this: