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"]
    
  • [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

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

  • Hello
    I added the github function and tried to add my shorcode but “XXX does not have any rows”. Any idea ?

    [acf_repeater field=”jour_x” sub_fields=”photo_jour_x,description_jour_x”]
    Photo: %photo_jour_x%
    Description: %description_jour_x%
    [/acf_repeater]

    Thanks
    Jonathan

  • @devfx – is there a way to modify this script of yours to include the current user instead of the current post so that in case of a repeater field placed on the backend user profile page you can output that data via a shortcode on the frontend.

  • I’m using ACF Options so the data isn’t sourced from the page/post – I’m struggling to get the code to read the data from the options table. I’m just getting “Services does not have any rows” message.

    How do I specify to get data from the options table?

  • Ok, I managed to deal with the code to output the repeater and subfields on the user’s profile page where the repeater fields are placed on the backend profile page of that user and make it in a nice table:

    function acf_repeater() {
    
       $user_id = get_current_user_id();
       ob_start(); ?>
       <?php if( have_rows('repeater-name',"user_{$user_id}" ) ): ?>
    
    	<table>
    	   <tr>
    	   	<td>Header1:</td><td">Header2:</td><td>Header3:</td><td>Header4:</td>
    		</tr>
    
    	<?php while ( have_rows('repeater-name', "user_{$user_id}" ) ) : the_row(); 
    
    		// vars
    		$subfieldname1 = get_sub_field('sub-field-name-1');
    		$subfieldname2 = get_sub_field('sub-field-name-2');
    		$subfieldname3 = get_sub_field('sub-field-name-3');
    		$subfieldname4 = get_sub_field('sub-field-name-3');
    
    	?>
    	   <tr>
    	      <td><?php echo $subfieldname1; ?></td><td><?php echo $subfieldname2; ?></td><td><?php echo $subfieldname3; ?></td><td><?php echo $subfieldname4; ?></td>
    	   </tr>
    		
    
    	<?php endwhile; ?>
    
    	</table>
    
    <?php endif; ?>
    <?php $output = ob_get_clean();
        return $output;
    }
    add_shortcode('acf_repeater_shortcode', 'acf_repeater');

    I hope this will be usefull to someone…

  • How do I display a specific row in this updated version?

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

    I’ve been trying to work out where to add the %ROW% function in the code (like in the original code) but cannot get it to work correctly.

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

The topic ‘Repeater field shortcode?’ is closed to new replies.