Support

Account

Home Forums General Issues Add multiple layouts to flexible content using acf_add_local_field(); Reply To: Add multiple layouts to flexible content using acf_add_local_field();

  • 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!