这是我试图通过管理面板创建字段时从ACF导出的代码:
function af_barbershop_address_field() {
if(function_exists("register_field_group"))
{
register_field_group(array (
'id' => 'acf_address',
'title' => 'Address',
'fields' => array (
array (
'key' => 'field_address',
'label' => 'address',
'name' => 'address',
'type' => 'textarea',
'required' => 1,
'default_value' => '',
'placeholder' => 'type the address',
'maxlength' => '',
'rows' => '',
'formatting' => 'br',
),
),
'location' => array (
array (
array (
'param' => 'post_type',
'operator' => '==',
'value' => 'barbershop',
'order_no' => 0,
'group_no' => 0,
),
),
),
'options' => array (
'position' => 'normal',
'layout' => 'no_box',
'hide_on_screen' => array (
),
),
'menu_order' => 0,
));
}
}
add_action( 'acf/init', 'af_barbershop_address_field' );我还试图将acf.php文件包含到functions.php文件中:
$acf_url = WP_PLUGIN_DIR . '/advanced-custom-fields/acf.php';
include_once( $acf_url );所有代码都在我的函数文件里。导出代码后,我删除了通过面板创建的字段,并尝试用代码创建完全相同的字段,但它不起作用。有什么问题吗?从上周起我就一直在处理这个问题。
发布于 2018-06-20 06:14:39
Action acf/init只适用于pro版本,我认为您忘记了使用免费版本,因为使用pro版本的代码工作得很好。
对于基本版本,您必须使用acf/register_fields注册自定义字段。
因此,您需要修改代码以:
function af_barbershop_address_field() {
if ( function_exists( "register_field_group" ) ) {
register_field_group( array(
'id' => 'acf_address',
'title' => 'Address',
'fields' => array(
array(
'key' => 'field_address',
'label' => 'address',
'name' => 'address',
'type' => 'textarea',
'required' => 1,
'default_value' => '',
'placeholder' => 'type the address',
'maxlength' => '',
'rows' => '',
'formatting' => 'br',
),
),
'location' => array(
array(
array(
'param' => 'post_type',
'operator' => '==',
'value' => 'barbershop',
'order_no' => 0,
'group_no' => 0,
),
),
),
'options' => array(
'position' => 'normal',
'layout' => 'no_box',
'hide_on_screen' => array(),
),
'menu_order' => 0,
) );
}
}
add_action( 'acf/register_fields', 'af_barbershop_address_field' );这应该没问题的。这将不适用于专业版,只有更早的代码才能工作。因此,您甚至可以将这两个操作挂钩,以便万一您将来进行升级,代码仍能工作。
https://wordpress.stackexchange.com/questions/306422
复制相似问题