Support

Account

Home Forums Add-ons Repeater Field Repeater field shortcode? Reply To: Repeater field shortcode?

  • [acf field="{$repeater_name}_{$row}_{$sub_field_name}"]

    I found this thread searching for ACF Repeater Shortcode and it was very helpful because I didn’t know that syntax above and it helped me created a shortcode to implement a loop that I think you all might find helpful.

    
    function my_acf_repeater($atts, $content='') {
      extract(shortcode_atts(array(
        "field" => null,
        "post_id" => null
      ), $atts));
      
      $_content = '';
      $row = 0;
      if( have_rows($field, $post_id) ):
        while ( have_rows($field, $post_id) ) : the_row();
          $_tmp = str_replace('%ROW%', $row, $content);
          $_content .= do_shortcode( $_tmp ) . '<br>';
          $row++;
        endwhile;
      else :  
        $_content = "$field does not have any rows";
      endif;
    
      return $_content;
    }
    
    add_shortcode("acf_repeater", "my_acf_repeater");
    

    and you can use it like this:

    
    [acf_repeater field="my-row"]
    name: [acf field='my-row_%ROW%_full-name']
    phone: [acf field='my-row_%ROW%_phone-num']
    [/acf_repeater]
    

    %ROW% will be replaced with current row number.

    What I’m working on next is updating the shortcode so that it can work like this:

    
    [acf_repeater field="my-row" sub_fields="full-name,phone-num"]
    name: %full-name%
    phone: %phone-num%
    [/acf_repeater]
    

    This will make it a little easier to read