Home › Forums › General Issues › Add multiple layouts to flexible content using acf_add_local_field();
I’m not sure if this is possible. The code below is not working.
The repeater field
<?php if( function_exists('acf_add_local_field_group') ) {
if( function_exists('acf_add_local_field_group') ):
acf_add_local_field_group(array (
'key' => 'group_5707100000b91',
'title' => 'add component fo (copy)',
'fields' => array (
array (
'key' => 'field_flex_key',
'label' => 'my content',
'name' => 'my_content',
'type' => 'flexible_content',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'button_label' => 'Add Row',
'min' => '',
'max' => '',
'layouts' => array (
//should import here
),
),
),
'location' => array (
array (
array (
'param' => 'post_type',
'operator' => '==',
'value' => 'post',
),
),
),
'menu_order' => 0,
'position' => 'normal',
'style' => 'default',
'label_placement' => 'top',
'instruction_placement' => 'label',
'hide_on_screen' => '',
'active' => 1,
'description' => '',
));
endif;
}
?>
The layouts i wish to import to the flexible content
<?php
acf_add_local_field(
array (
'key' => 'layout_key_1',
'name' => 'sample_label',
'label' => 'sample label',
'display' => 'block',
'parent' => 'field_flex_key', //flex field key
'sub_fields' => array (
array (
'key' => 'field_5707100b6c',
'label' => 'sample',
'name' => 'sample 2',
'type' => 'text',
'parent_layout' => 'layout_key_1', // layout key
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'maxlength' => '',
'readonly' => 0,
'disabled' => 0,
),
),
'min' => '',
'max' => '',
)
);
acf_add_local_field(
array (
'key' => 'layout_key_2',
'name' => 'sample_label12',
'label' => 'sample label',
'display' => 'block',
'parent' => 'field_flex_key', //flex field key
'sub_fields' => array (
array (
'key' => 'field_5707100006b6c',
'label' => 'sample',
'name' => 'sample',
'type' => 'text',
'parent_layout' => 'layout_key_2', // layout key
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'maxlength' => '',
'readonly' => 0,
'disabled' => 0,
),
),
'min' => '',
'max' => '',
)
);
Hi @pixelstorm
I’m afraid you can’t use the acf_add_local_field() function to add custom fields to a flexible content with multiple layouts. What you can do is to store the flexible settings in a variable, modify it as you want and then call the acf_add_local_field_group() later.
I hope this makes sense.
Hi @pixelstorm
I believe you can do it like this:
if( function_exists('acf_add_local_field_group') ):
$args = array (
'key' => 'group_5707100000b91',
'title' => 'add component fo (copy)',
'fields' => array (
array (
'key' => 'field_flex_key',
'label' => 'my content',
'name' => 'my_content',
'type' => 'flexible_content',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'button_label' => 'Add Row',
'min' => '',
'max' => '',
'layouts' => array (),
),
),
'location' => array (
array (
array (
'param' => 'post_type',
'operator' => '==',
'value' => 'cpt',
),
),
),
'menu_order' => 0,
'position' => 'normal',
'style' => 'default',
'label_placement' => 'top',
'instruction_placement' => 'label',
'hide_on_screen' => '',
'active' => 1,
'description' => '',
);
$layout_1 = array (
'key' => 'layout_key_1',
'name' => 'sample_label',
'label' => 'sample label',
'display' => 'block',
'parent' => 'field_flex_key', //flex field key
'sub_fields' => array (
array (
'key' => 'field_5707100b6c',
'label' => 'sample',
'name' => 'sample 2',
'type' => 'text',
'parent_layout' => 'layout_key_1', // layout key
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'maxlength' => '',
'readonly' => 0,
'disabled' => 0,
),
),
'min' => '',
'max' => '',
);
$layout_2 = array (
'key' => 'layout_key_2',
'name' => 'sample_label12',
'label' => 'sample label 2',
'display' => 'block',
'parent' => 'field_flex_key', //flex field key
'sub_fields' => array (
array (
'key' => 'field_5707100006b6c',
'label' => 'sample',
'name' => 'sample',
'type' => 'text',
'parent_layout' => 'layout_key_2', // layout key
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'maxlength' => '',
'readonly' => 0,
'disabled' => 0,
),
),
'min' => '',
'max' => '',
);
$args['fields'][0]['layouts'][] = $layout_1;
$args['fields'][0]['layouts'][] = $layout_2;
acf_add_local_field_group($args);
endif;
Hope this helps!
You must be logged in to reply to this topic.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.