Support

Account

Forum Replies Created

  • I updated the code to run as I mentioned above. You can find it here:

    https://gist.github.com/FranciscoG/c393d9bc6e0a89cd79d1fd531eccf627

    I also forgot to mention that it will parse shortcodes within the repeater rows

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

    Caveat: You can not do nested repeater rows. However you need to you can just duplicate the add_shortcode and add more shortcode repeaters shortcodes and it will work. There’s probably a much better way to handle unrestricted amount of sub-repeaters but that’s a lot more work and testing for another day

    
    add_shortcode("acf_repeater", "my_acf_repeater");
    add_shortcode("acf_sub_repeater", "my_acf_repeater");
    
  • [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

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