Support

Account

Home Forums ACF PRO Display data from specific repeater row

Solving

Display data from specific repeater row

  • Hey team!

    I am looking for a way to display two things in my WP theme, but it’s getting quite complex and I can’t track down any documentation of doing this.

    I have a repeater field called ‘locations’ setup with 5 subfields (name, phone, street_address, state, postcode). At the moment, I have 2 rows all containing values. EG:

    Name 1
    Phone 1
    Street Address 1, State 1, Postcode 1

    ——

    Name 2
    Phone 2
    Street Address 2, State 2, Postcode 2

    ——

    I need the ability to split out this information for displaying on different pages. How can I:

    1/ Display all sub fields from a specific row? EG. Show all data from row 2.

    2/ Display a specific sub field from a specific row? EG. Show ‘phone’ from row 2.

    Hopefully someone out there can shed some light on this!

  • There is only one way to do this using acf functions. That is to loop through the repeater and output the row or field that your looking for. This can be done with counters

    
    if (have_fields('repeater')) {
      $counter = 0;
      while (have_rows('repeater')) {
        the_row();
        $counter++;
        // at this point the counter is set to the current row
        // you can output content or not based on that row
        // for example
        if ($row == 4) {
          // output content from row 4
        }
      }
    }
    

    You can also check values of the fields in the row to see if you want to show that row.

  • You can also go this way

    $rows = get_field('repeater' ); // get all the rows
    $specific_row = $rows[YOUR_ROW_NUMBER]; // 0 will get the first row, remember that the count starts at 0
    $sub_field_value = $specific_row['SUB_FIELD_NAME']; // get the sub field value

    This also explained on the tutorial page for repeaters https://www.advancedcustomfields.com/resources/repeater/

  • is there the possibility to show the title of a file repeater field ::.

    So to show the title of the row? Like in an image field.

    with print_r i am getting this: `Array
    (
    [downloads_acf_field_single_product] => Array
    (
    [ID] => 1072
    [id] => 1072
    [title] => Product brochure
    [filename] => M-107-M-111-Alfa-Romeo-8c-DE.pdf`

    So how can i extract the [title]

  • As simple as this:

    $rows = get_field('repeater' ); // get all the rows
    $specific_row = $rows[YOUR_ROW_NUMBER]; // 0 will get the first row, remember that the count starts at 0
    $title = $specific_row['SUB_FIELD_NAME']['title];

    In your specific example the SUB_FIELD_NAME would be downloads_acf_field_single_product

  • Hello everybody,
    I came across these threads and I am currently trying on the same topic. My repeater sits inside an option page and is queried by within a flexible layout. Unfortunately, it does not work that way, what’s wrong with the code?

    <?php if (get_sub_field('sidebar_submenus') == 'sm-1') : ?>	
    <?php
    $rows = get_field('submenus', 'option' );
    $specific_row = $rows[0];
    $sub_field_value = $specific_row['submenus_content'];
    $submenu = get_field( $sub_field_value, 'option' );
    ?>
    <?php echo $submenu[0]; ?>	  
    <?php endif; ?> 

    Thank you very much in advance
    lexx

  • @Thomas

    This method works perfectly. The only downside is you’d need to update your code if you changed the position of the row.

  • FYI you can also do something like:

    the_field( 'repeater_3_phone' );

    The 3 here is a zero-based index, so this should output the phone field from row 4.

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

The topic ‘Display data from specific repeater row’ is closed to new replies.