Support

Account

Home Forums ACF PRO Getting first row of Repeater with the_row_index()

Solved

Getting first row of Repeater with the_row_index()

  • I noticed that the_row_index() was introduced recently for the repeater field and wondered if it could be used to get the content from the first row of a repeater.

    i/e you have a repeater with a row which has image and text, and you grab just the first image from that first row.

    I cannot find any further documentation on the_row_index() so unsure how to even give an example.

  • The function will only return the current index of the active loop. Basically you cannot access fields in a repeater without having an active have_rows() loop. This works the same as the WP have_posts() loop. Without a loop, many of the functions in WP will not work because they are only designed to work inside the loop.

    
    // accessing only the first row of a repeater
    if (have_rows('repeater')) {
      while (have_rows('repeater')) {
        the_row();
        the_sub_field('sub_field_name');
        // after doing what you want to do with the fist element
        break;
      }
    }
    // if you want you can reset the above row to 
    // begin from the first row again
    reset_rows();
    // start another loop on the same field
    // if you don't use reset above then it will
    // start with the second row
    if (have_rows('repeater')) {
      // etc....
    }
    
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Getting first row of Repeater with the_row_index()’ is closed to new replies.