Home › Forums › Add-ons › Repeater Field › Creating default subfields in repeater field › Reply To: Creating default subfields in repeater field
What I am actually doing is just loading default subfields (with no values) into a repeater. Just empty subfields the user can field out.
So basically, I am pre-adding subfields to a repeater, so I do not have to add the fields manually.
The subfields are created automatically using acf/load_field/type=flexible_content
.
function acf_flexible_repeater_subfields( $field ) {
$field['sub_fields'] = array(
array(
'key' => 'headline',
'label' => 'Headline',
'name' => 'headline',
'type' => 'text'
)
)
}
add_filter('acf/load_field/type=repeater', 'acf_flexible_repeater_subfields');
Now, this works perfectly. Keep in mind – doing it this way does not allow you to add child fields via ACF. You will have to add it within the same acf/load_field/type=repeater
hook.
I believe you want to add default values – which you can do like this:
function acf_flexible_repeater_subfields( $field ) {
$field['sub_fields'] = array(
array(
'key' => 'headline',
'label' => 'Headline',
'name' => 'headline',
'type' => 'text',
'default_value' => 'Hi' // ADD THIS LINE
)
)
}
add_filter('acf/load_field/type=repeater', 'acf_flexible_repeater_subfields');
Test it out in functions.php if that is what you are trying to achieve.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.