Support

Account

Home Forums Add-ons Repeater Field Repeater Field Shortcode Display certain rows

Solving

Repeater Field Shortcode Display certain rows

  • I am using this shortcode but it does not allow me to display certain rows of the repeater fields post id. How can I accomplish this?

    /**
     * ACF Pro repeater field shortcode
     * 
     * @attr {string} field - (Required) the name of the field that contains a repeater sub group
     * @attr {string} sub_fields - (Required) a comma separated list of sub field names that are part of the field repeater group
     * @attr {string} post_id - (Optional) Specific post ID where your value was entered. Defaults to current post ID (not required). This can also be options / taxonomies / users / etc
     */
    function my_acf_repeater($atts, $content='') {
      extract(shortcode_atts(array(
        "field" => null,
        "sub_fields" => null,
        "post_id" => null
      ), $atts));
      if (empty($field) || empty($sub_fields)) {
        // silently fail? is that the best option? idk
        return "";
      }
      $sub_fields = explode(",", $sub_fields);
      
      $_finalContent = '';
      if( have_rows($field, $post_id) ):
        while ( have_rows($field, $post_id) ) : the_row();
          
          $_tmp = $content;
          foreach ($sub_fields as $sub) {
            $subValue = get_sub_field(trim($sub));
            $_tmp = str_replace("%$sub%", $subValue, $_tmp);
          }
          $_finalContent .= do_shortcode( $_tmp );
        endwhile;
      else :  
        $_finalContent = "$field does not have any rows";
      endif;
      return $_finalContent;
    }
    add_shortcode("acf_repeater", "my_acf_repeater");
  • Hi @dkeys222

    Thanks for the mail.

    Unfortunately, its not quite clear on the issue that you are having.

    Are you able to return all the rows from the repeater? Are you getting any errors?

    Kindly share more details on the problem that you are having so that we may be able to provide more specific support.

    Hope to hear from you soon.

  • That code is working fine. For example when I use the short code I use:

    [acf_repeater field="inventory-8" post_id="32063" sub_fields="system"]
    %system%[/acf_repeater]

    That code displays all the repeater fields of that specific post id with that subfield.

    But how can I get it to display only 1 row?

  • That code is working fine. For example when I use the short code I use:

    [acf_repeater field="inventory-8" post_id="32063" sub_fields="system"]
    %system%[/acf_repeater]

    That code displays all the repeater fields of that specific post id with that subfield.

    But how can I get it to display only 1 row?

  • @dkeys222 I have the exact same issue. How do display only 1 row. now it returns the correct result but multiplied with the rows count. For example if i have 10 rows in the repeater, it returns the correct result but 10 times. have you worked it out?

    @acf-support

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

The topic ‘Repeater Field Shortcode Display certain rows’ is closed to new replies.