Support

Account

Home Forums Add-ons Repeater Field Repeater field Specific row

Unread

Repeater field Specific row

  • Hi,

    I’m a very beginner in PHP & ACF PRO.
    I store several row in a repeater field.
    I would like to be able to “call” all fields of any specific row by using a shortcode.

    I’ve found this one:

    /**
     * ACF Pro repeater field shortcode
     *
     * I created this shortcode function because it didn't exist and it was being requested by others
     * I originally posted it here: https://support.advancedcustomfields.com/forums/topic/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");

    it ables me to get datas using shortcode like this:

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

    The only problem of this Shortcode is that I can’t set a specific row. In fact, it call all rows of the repeater.

    I would love to be able to add a param in the shortcode to set the ROW NUMBER…

    I did not find anything like this into topics that relates of this point…

    Could you help me please?
    That’s so frustrating to no be able to use Repeater field since I can not call datas I store into it =/

    Thanks you so much…

Viewing 1 post (of 1 total)

The topic ‘Repeater field Specific row’ is closed to new replies.