当前位置: 首页 > 知识库问答 >
问题:

ACF 以编程方式添加中继器

苏坚成
2023-03-14

我正在使用高级自定义字段(ACF ),并尝试以编程方式向现有组(group_5621b0871e1b1)添加一个中继器,但它不起作用。相同的代码适用于文本字段,但不适用于repeater。

在我的插件中:

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

我做错什么了吗?有没有可能以编程方式添加一个中继器。

共有1个答案

郁明诚
2023-03-14

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

  • 删除所有子字段,包括'子字段'=

您的代码现在看起来像:

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,
        ));
    }
}

 类似资料:
  • 我正在尝试在Android上添加Wifi网络,我想知道如何连接到不广播其SSID的Wifi网络(它是否有空SSID或带有\0s的清晰SSID)。 这是我目前用于广播其SSID的Wifi网络的内容:

  • 问题内容: 我想在头部分中以编程方式添加StyleSheets,但是我看到的示例之一似乎需要多行代码才能仅添加一个样式表,即使我可能需要很多: 示例代码: 我也使用方法,但是它也不起作用。对象null抛出了错误。 我也使用了和东西,但是它们抛出了文字错误,这是我认为的常见错误。 我使用此代码: 起初它起作用,但是当我更改页面时,它停止工作。 我正在使用“母版页”,并且正在文件中编写这些代码,也有人

  • 我试图在logback中动态添加一个appender。这是我的代码。 它工作正常,但仅适用于添加追加器的特定记录器。有没有办法让它适用于应用程序中的所有记录器?我正在寻找一种动态添加和删除追加器的方法。

  • 我有一个JPA(Hibernate)项目,其中包含从XML orm.XML和Beans创建的实体,我有一个EntityManager和update、persist、remove、findById、findAll方法,但我需要添加一个过滤器监听器来检查保存、还原等之前和之后的Beans(我需要将一些值设置为null以返回客户机) 问候。

  • 我有一个模型,其中有一个@列(nullable=false)注释HiberNate和所有字段有nullable=false,我想以编程方式添加一些新的注释,如@NotNull和@ApiModelProperty(必需=true)-用于招摇过市。 所以,我希望能够从我的应用程序的模型中解析所有字段,获得现有的注释,并在此基础上添加新的注释。这能做到吗? 更新:问题是每次添加一个新字段,如果它不能为空

  • 我正在使用SpringDoc,并试图以编程方式向OpenApi添加一个模式,但没有成功。 mySchema的描述没有添加到我在生成的YAML文件中看到的模式列表中,如果我试图引用它: