Support

Account

Home Forums Add-ons Repeater Field Repeater field shortcode?

Solving

Repeater field shortcode?

  • Is it possible to use a shortcode to display a repeater group and its fields, instead of using php? I have tried the standard [acf field="{$field_name}"] but nothing shows up. Thank you.

  • Actually, yes there is.

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

    Rows start at zero, not one.

    But there isn’t any way to create a loop to show them I don’t think.

  • @hube2 Wow, thank you for the quick response. I’m sure it’s evident I’m pretty new to using ACF in templates. When you say there isn’t a way to create a loop, what do you mean?

    I am making a site where someone else will put in info about sports teams (a custom post type), and for the repeater field, they can add as many coaches per sport as they need, but not every sport will have the same number, which is why I chose the repeater. And I’m trying to make page templates in the theme’s page builder so the person who manages the site just has to type things into the meta fields instead of opening the page template and editing that.

  • In PHP, if I were to edit the template I would create a loop for the repeater.

    
    <?php 
      if (have_rows($repeater_field)) {
        while (have_rows($repeater_field)) {
          the_row;
          the_sub_field($sub_field_name);
        }
      }
    ?>
    

    This could be use to create a list of items or other layouts depending on what you’re doing with it.

    I don’t know how your theme’s page builder works, or how you’d go about doing that in your theme. I don’t use themes with page builders, I generally build custom themes and I’d just build a template for the custom post type and code the entire thing by hand.

    The ACF shortcode really isn’t meant for repeater fields, but it can be done if you know the field name stored used to store the field in the database.

  • I got as far as to make the custom post type and re-create/assign the page template, which uses the page builder, for the post type. I want some of each sport page to be editable, but some parts of it to output strictly the acf fields that are shown. So I’ve added a code section in the page builder, and so far have managed to get this, with your help, in that part…

    <h3>Coaching Staff</h3>
    <div class="coach_name">[acf field="coaching_staff_0_coachs_name"], [acf field="coaching_staff_0_coachs_title"]</div>
    
    <div class="coach_contact">Phone: [acf field="coaching_staff_0_coachs_phone"] | <a href='mailto:[acf field="coaching_staff_0_coachs_email"]'>E-Mail</a></div>
    
    <div class="coach_bio">[acf field="coaching_staff_0_coachs_biography"]</div>

    I don’t know enough about building page templates or php to create the full page that way, wish I did! I’ve hired people to make them for some other sites, but had a little extra time and wanted to try to learn a little more 😉

  • Learning more is good. I think your best source of information will be the authors of the framework and theme you’re using when it comes to making changes to them. Most of the ones I’ve seen use filters to let you modify the output in templates, but that’s just a guess.

  • Hi John / ericaeide

    Chances are that this wont get answered as its been 2 years since the initial post but you never know 🙂

    Im having issue calling a repeater field with the above code can you have a quick look at what im doing and let me know where I have gone wrong?

    —-SET Up—-

    Created New Field Group called test list and attached it to a page using the rules

    The first item is called list items (field name list_items) this is set to repeater

    The next field in the repeater is called item and its just plain text (field name item)
    The next field in the repeater is called item image its just plain image upload (field name item_image)

    The short code im using to call this onto another page is as follows

    [acf field="list_items_0_item"]

    It calls on the list page but i cant seem to pull that list into another page by using that shortcode do I need to reference the page id?

    Should this be returning all the items I have entered?

    Thanks in advance for your help if you get this 🙂

  • It will only show one item and image fields will not work in shortcodes. If you want to so all of the rows and you want to show more than just basic field types then you’ll need to write your own shortcodes. https://codex.wordpress.org/Function_Reference/add_shortcode

  • Thanks a million John managed to figure it out in the end I was missing the post id at the end of the short code call
    Cheers for coming back to me on this really didnt expect it after 2 years thanks again 🙂
    Heres how the code looked in the end

    <img src="[acf field='list_of_sdgs_0_sdg_icon_image']" /> <a href="[acf field=">[acf field="list_of_sdgs_0_sdg_name"]</a>: [acf field="list_of_sdgs_0_sdg_purpose"]
    
    • DevFx

    • March 27, 2018 at 6:54 am

    [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

    • DevFx

    • March 28, 2018 at 9:34 am

    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");
    
    • gb3k

    • October 29, 2018 at 8:26 am

    Hi @devfx,
    Many thanks for your script!
    It works great!
    Best,

    Gustavo

  • Thanks for the script, @devfx. How can I display a specific row? Also, taxonomies show Array! is there anyway to fix it?

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

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.