Support

Account

Home Forums Add-ons Repeater Field Setting Repeater Defaults Per Page Template Reply To: Setting Repeater Defaults Per Page Template

  • Hi there,

    Here’s an example that allows you to set defaults in a repeater dynamically, for when there is no current value:
    .. change the ‘key’ value in the filter call… you can expose the ‘key’ when editing your fields by checking the box in Screen Options (top right)

    
    <?php
    function my_default_value_subs($value, $post_id, $field) {
      if ($value === false) {
        $value = array(
          array(
            'field_58984ddb5b8cc' => 'My title',
            'field_58985045abac0' => 'My content',
          ),
          array(
            'field_58984ddb5b8cc' => 'Another title for a content-less row',
          ),
        );
      }
      return $value;
    }
    add_filter('acf/load_value/key=key_58c7ee3a37aee', 'my_default_value_subs', 10, 3);
    ?>
    

    Now… to make it conditional based on a page template.. you could use the function:

    get_page_template_slug( get_the_ID() );

    This returns the ‘slug’ of the page template…

    So… they would choose the template first… save the post (as Draft or whatever), and then the defaults would be set if you modified the above code to be like:

    
    <?php
    function my_default_value_subs($value, $post_id, $field) {
      if ($value === false) {
        switch (get_page_template_slug( get_the_ID() )) {
          case 'about.php':
            $value = array(
              array(
                'field_58984ddb5b8cc' => 'My title',
                'field_58985045abac0' => 'My content',
              ),
              array(
                'field_58984ddb5b8cc' => 'Another title for a content-less row',
              ),
            );
            break;
          case 'other-template.php':
            $value = array(
              array(
                'field_58984ddb5b8cc' => 'My title',
                'field_58985045abac0' => 'My content',
              ),
              array(
                'field_58984ddb5b8cc' => 'Another title for a content-less row',
              ),
            );
            break;
          case 'other-template-slug.php':
            $value = array(
              array(
                'field_58984ddb5b8cc' => 'My title',
                'field_58985045abac0' => 'My content',
              ),
              array(
                'field_58984ddb5b8cc' => 'Another title for a content-less row',
              ),
            );
            break;
        }
      }
      return $value;
    }
    add_filter('acf/load_value/key=key_58c7ee3a37aee', 'my_default_value_subs', 10, 3);
    ?>
    

    Your $value would be different depending on the ‘type’ of subfields, but this should get you pointed in the right direction I hope!

    Let me know,
    Keith