这是我在尝试通过管理面板创建字段时从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文件转换为我的函数。php文件:
$acf_url = WP_PLUGIN_DIR . \'/advanced-custom-fields/acf.php\';
include_once( $acf_url );
所有代码都在我的函数文件中。导出代码后,我删除了通过面板创建的字段,并尝试使用代码创建完全相同的字段,但它不起作用。问题是什么?从上周开始我就一直在处理这个问题。
最合适的回答,由SO网友:Kumar 整理而成
行动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\' );
这应该很好。这对Pro版本不起作用,只有早期的代码才起作用。因此,您甚至可以钩住这两个操作,以便在将来碰巧升级时,代码仍然可以工作。