gpt4 book ai didi

wordpress - ACF 以编程方式添加转发器

转载 作者:行者123 更新时间:2023-12-04 00:10:53 28 4
gpt4 key购买 nike

我正在使用 Advanced Custom Fields (ACF) 并尝试以编程方式将中继器添加到现有组 (group_5621b0871e1b1),但它不起作用。相同的代码适用于文本字段,但不适用于转发器。

在我的插件中:

add_action( 'acf/init', 'acf_add_field_royalties' );
function acf_add_field_royalties() {
if ( function_exists( 'acf_add_local_field_group' ) ) {
acf_add_local_field( array (
'key' => 'field_store_royalties',
'label' => 'Royalties',
'name' => 'store_royalties1',
'type' => 'repeater',
'parent' => 'group_5621b0871e1b1',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'collapsed' => '',
'min' => '',
'max' => '',
'layout' => 'table',
'button_label' => 'Add new royalty period',
'sub_fields' => array (
array (
'key' => 'field_start_date',
'label' => 'Start Date',
'name' => 'start_date1',
'type' => 'date_picker',
'instructions' => '',
'required' => 1,
'display_format' => 'F j, Y',
'return_format' => 'd/m/Y',
'first_day' => 1,
),
array (
'key' => 'field_end_date',
'label' => 'End date',
'name' => 'end_date1',
'type' => 'date_picker',
'instructions' => '',
'display_format' => 'F j, Y',
'return_format' => 'd/m/Y',
'first_day' => 1,
),
array (
'key' => 'field_royalty_rate',
'label' => 'Royalty Rate',
'name' => 'royalty_rate1',
'type' => 'number',
'instructions' => '',
'required' => 1,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => 0,
'placeholder' => '',
'prepend' => '',
'append' => '%',
'min' => 0,
'max' => 100,
'step' => 1,
'readonly' => 0,
'disabled' => 0,
)
)
));
}
}

它在 group_5621b0871e1b1 组中显示此错误:

Warning: Invalid argument supplied for foreach() in /usr/share/nginx/html/wordpress4/wp-content/plugins/advanced-custom-fields-pro/pro/fields/repeater.php on line 255
Warning: Invalid argument supplied for foreach() in /usr/share/nginx/html/wordpress4/wp-content/plugins/advanced-custom-fields-pro/pro/fields/repeater.php on line 320

我做错了什么吗?是否可以通过编程方式添加转发器。

最佳答案

由于转发器字段是使用acf_add_local_field添加的,因此每个子字段也需要添加acf_add_local_field

  • 删除所有子字段,包括 'subfields' => array线,来自中继场。
  • 在新的 acf_add_local_field 中添加每个子字段
  • 添加子字段时,确保将 'parent' => 'repeater_field_key' 添加到子字段。

您的代码现在看起来像:

add_action( 'acf/init', 'acf_add_field_royalties' );

function acf_add_field_royalties() {
if ( function_exists( 'acf_add_local_field_group' ) ) {
/**
* Initial Repeater Field
*
*/
acf_add_local_field( array (
'key' => 'field_store_royalties',
'label' => 'Royalties',
'name' => 'store_royalties1',
'type' => 'repeater',
'parent' => 'group_5621b0871e1b1',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'collapsed' => '',
'min' => '',
'max' => '',
'layout' => 'table',
'button_label' => 'Add new royalty period'
));

/**
* Add Start Date Subfield
*
*/
acf_add_local_field( array (
'key' => 'field_start_date',
'label' => 'Start Date',
'name' => 'start_date1',
'parent' => 'field_store_royalties', // key of parent repeater
'type' => 'date_picker',
'instructions' => '',
'required' => 1,
'display_format' => 'F j, Y',
'return_format' => 'd/m/Y',
'first_day' => 1,
));

/**
* Add End Date Subfield
*
*/
acf_add_local_field( array (
'key' => 'field_end_date',
'label' => 'End date',
'name' => 'end_date1',
'parent' => 'field_store_royalties', // key of parent repeater
'type' => 'date_picker',
'instructions' => '',
'display_format' => 'F j, Y',
'return_format' => 'd/m/Y',
'first_day' => 1,
));

/**
* Add Royalty Rate Subfield
*
*/
acf_add_local_field( array (
'key' => 'field_royalty_rate',
'label' => 'Royalty Rate',
'name' => 'royalty_rate1',
'parent' => 'field_store_royalties', // key of parent repeater
'type' => 'number',
'instructions' => '',
'required' => 1,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => 0,
'placeholder' => '',
'prepend' => '',
'append' => '%',
'min' => 0,
'max' => 100,
'step' => 1,
'readonly' => 0,
'disabled' => 0,
));
}
}

关于wordpress - ACF 以编程方式添加转发器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35780165/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com