ACF fügt Repeater programmgesteuert hinzu

Lesezeit: 4 Minuten

Benutzer-Avatar
Oterox

Ich benutze Erweiterte benutzerdefinierte Felder (ACF) und versuchen, einen Repeater programmgesteuert zu einer vorhandenen Gruppe hinzuzufügen (group_5621b0871e1b1), aber es funktioniert nicht. Derselbe Code funktioniert für ein Textfeld, aber nicht für den Repeater.

In meinem Plugin:

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

Es zeigt diesen Fehler in der Gruppe 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

Mache ich etwas falsch? Ist es möglich, einen Repeater programmgesteuert hinzuzufügen.

  • Haben Sie diese Frage in den ACF-Supportforen gestellt?

    – Bärkins

    27. Juli 2016 um 15:20 Uhr

Da das Repeater-Feld mit hinzugefügt wurde acf_add_local_fieldjedes Unterfeld muss mit hinzugefügt werden acf_add_local_field auch.

  • Löschen Sie alle Unterfelder, einschließlich der 'subfields' => array
    Linie, aus dem Repeater-Feld.
  • Fügen Sie jedes Unterfeld in einem neuen hinzu acf_add_local_field
  • Stellen Sie beim Hinzufügen von Unterfeldern sicher, dass Sie hinzufügen 'parent' => 'repeater_field_key' zu den Unterfeldern.

Ihr Code würde jetzt so aussehen:

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

  • Ich konnte das nicht zum Laufen bringen, und wenn ich ein ACF über die Benutzeroberfläche generierte und es über Tools -> Generate PHP exportierte, wurden sub_fields verwendet.

    – Lukas Bustamante

    10. September 2019 um 14:33 Uhr

1094450cookie-checkACF fügt Repeater programmgesteuert hinzu

This website is using cookies to improve the user-friendliness. You agree by using the website further.

Privacy policy