Support

Account

Home Forums Add-ons Repeater Field Shortcode to display ACF repeater field from Options table

Solving

Shortcode to display ACF repeater field from Options table

  • I’ve a repeater field in the ACF pro options table (so for global use) and I want to be able to display specific data from the repeater fields.

    EG: I have services repeater field, and in there is service_name, service_description, service_image

    I’ve tried adding some function:

    function my_acf_repeater($atts, $content='') {
      extract(shortcode_atts(array(
        "field" => null,
      ), $atts));
      
      $_content = '';
      $row = 0;
      if( have_rows($field) ):
        while ( have_rows($field,'options') ) : 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");

    Trying to specify to pull the data from the “option” table but it’s not working. I’m just getting “services does not have any rows”

    How can I get the specific data I need?

  • you need to add “options” to the if statement as well as the while
    if( have_rows($field, 'options') ):

  • Hi John,

    Thank you for pointing this out. I’ve added “option” to the IF statement too.

    However, I’m getting no data returned from my shortcode.

    So I have a repeater, “services” with sub-fields of “service-name”, “service-description-short” and “service-description-long”

    I just want the “name” and the “short description” so using the shortcode:

    [acf_repeater field="services"]
    service name: [acf field='services_1_service-name']
    service desc: [acf field='services_1_service-description-short']
    [/acf_repeater]

    What is displayed is:

    service name: service desc:

    It’s not populating with the data from my repeater second row (I’ve tried it with %ROW% 0 for the first row too, that’s the same result)

    What am I not getting?

  • Wow… the solution was SOOOOOO simple.

    I do not even need to add any code to my functions.php

    All I needed was to add the post_id=”options” to the shortcode.

    [acf field= "services_0_service-name" post_id="options"]

    The answer is always easier than you think!

Viewing 4 posts - 1 through 4 (of 4 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.