add_filter('acf/load_fields', 'initialize_fields');
function initialize_fields($fields) {
global $post;
if($post) {
acf_form_head();
}
return $fields;
}
$prefix = 'prefix_';
$custom_fields = array(
array(
'key' => $prefix . 'fieldname',
'label' => 'Field Label',
'name' => 'field_name',
'type' => 'text',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'maxlength' => '',
'readonly' => 0,
'disabled' => 0,
)
);
acf_add_local_field_group(array(
'key' => 'group_1',
'title' => 'Group Title',
'fields' => $custom_fields,
'location' => array(
array(
array(
'param' => 'post_type',
'operator' => '==',
'value' => 'post',
),
array(
'param' => 'post_status',
'operator' => '==',
'value' => 'publish',
),
),
array(
array(
'param' => 'post_type',
'operator' => '==',
'value' => 'page',
),
array(
'param' => 'post_status',
'operator' => '==',
'value' => 'publish',
),
),
),
'menu_order' => 0,
'position' => 'normal',
'style' => 'default',
'label_placement' => 'top',
));
上述代码会在文章和页面发布时初始化一个名为 "Field Label" 的文本字段。
注意:在使用此方法时,请确保已开启 ACF Options 页面中的'保存并显示所有字段组位置”选项。
上一篇:ACF | 有关条件字段值的问题