Home Forums Add-ons Repeater Field Get value from next row in repeater


Get value from next row in repeater

  • Is it possible to get the value of a field from the next and previous rows?

  • Not if your using a have_rows() loop to loop through the repeater.

    You can get the entire repeater field as an array value and then you can create code to look at the previous or next array element

    $repeater = get_field('repeater_field_name');
  • Hi @johnhuebner

    Im stuck on something similar where I need to get the field of the previous and next row using the foreach. Can you give me any advice to please?

  • Getting the previous and next row would depend on what you want to do at the ends of the row. For example, if the current row is the first row of the repeater do you want to show nothing for previous or do you want to show the last row?

    The first think you need to know in your loop is the current index, you do that by adjusting the loop

    foreach( $blocks as $index => $block ) :

    Next it depends on what you want to do. To show nothing before and after at the beginning/end

    // previous row
    if ($index > 0) {
      $previous_row_value = $blocks[$index-1]['field_name'];
    // next row
    if ($index+1 < count($blocks)) {
      $next_row_value = $blocks[$index+1]['field_name'];

    if you want to have a continuous loop then you need to calculate the next and previous index

    $prev_index = $index - 1;
    if ($prev_index < 0) {
      $prev_index = count($blocks)-1;
    $next_index = $index + 1;
    if ($next_index >= count($blocks)) {
      $next_index = 0;
  • Hi John thanks a lot for you comments. Im not at my desk at the moment to test so will update later.

    Ideally I’d want the first to show the last and the last to show the first.

  • Hi John

    Thanks for your help, nearly there!

    Im trying to do the continuous loop so I can show the last on the first and first on the last.

    Below is my foreach set up

    <?php foreach( $blocks as $index => $block ) :
    // ACF Vars
    $image		= $block['fb_main_image'];
    $imagePos	= $block['fb_main_image_fp'];
    $title 		= $block['fb_title'];
    $text		= $block['fb_text'];
    $link 		= $block['fb_link'];
    $linkText   = $block['fb_link_text'];
    // Previous row title
    $prevIndex	= $index - 1;
    if( $prevIndex < 0 ) :
    	$prevIndex = count($blocks)-1;
    $previous_row_value = $blocks[$index-1]['fb_title'];
    // Next row title
    $nextIndex	= $index + 1;
    if( $nextIndex >= count($block) ) :
    	$nextIndex = 0;
    $next_row_value = $blocks[$index+1]['fb_title'];

    Im getting an error Undefined offset: -1 for $previous_row_value = $blocks[$index-1]['fb_title']; and Undefined offset: 5 for $next_row_value = $blocks[$index+1]['fb_title'];

  • You have this

    $previous_row_value = $blocks[$index-1]['fb_title'];

    it should be

    $previous_row_value = $blocks[$prevIndex]['fb_title'];

    the same for next, you are using $index+1 but should be using the calculated value $nextIndex

  • Hi John, again thanks for your help so far. Editing the 2 $previous_row_value and $next_row_value to have $prevIndex and $nextIndex still gives me a error of

    Undefined offset: -1 for $previous_row_value = $blocks[$prevIndex-1]['fb_title']; for the first row


    Undefined offset: 5 for $next_row_value = $blocks[$nextIndex+1]['fb_title']; on the last row

    I’ve also noticed its now pulling in the 2nd row previous title now and not the previous row.

Viewing 8 posts - 1 through 8 (of 8 total)

The topic ‘Get value from next row in repeater’ is closed to new replies.