【问题标题】:get_sub_field on repeater returns empty keys/values中继器上的 get_sub_field 返回空键/值
【发布时间】:2018-07-05 19:46:38
【问题描述】:

编辑:更新了输出数组的代码

应该注意我还有其他可以正常工作的字段,问题仅在于转发器子字段。

// Needs to be updated to be more dynamic i know
function get_flexible_content_sections($name, $option = "") {
    // check if the flexible content field has rows of data
    if ( have_rows( $name, $option ) ):

        // loop through the rows of data
        while ( have_rows( $name, $option ) ) : the_row(); ?>

            <section class="section <?= get_sub_field( "class" ) ?>">
                <div class="section-content clear">

                    <?php while ( have_rows( "section" ) ) : the_row();
                        if ( get_row_layout() == 'one_width_cols' ) : ?>

                            <div class="full-width-col cols-12">
                                <?php while ( have_rows( "module_1" ) ) : the_row();
                                    get_layout_modules( get_row_layout() );
                                endwhile; ?>
                            </div>

                        <?php elseif ( get_row_layout() == 'two_width_cols' ) : ?>

                            <div class="half-width-col col-1 cols-6">
                                <?php while ( have_rows( "module_1" ) ) : the_row();
                                    get_layout_modules( get_row_layout() );
                                endwhile; ?>
                            </div>
                            <div class="half-width-col col-2 cols-6 last">
                                <?php while ( have_rows( "module_2" ) ) : the_row();
                                    get_layout_modules( get_row_layout() );
                                endwhile; ?>
                            </div>

                        <?php elseif ( get_row_layout() == 'three_width_cols' ) : ?>

                            <div class="three-width-col col-1 cols-4">
                                <?php while ( have_rows( "module_1" ) ) : the_row();
                                    get_layout_modules( get_row_layout() );
                                endwhile; ?>
                            </div>
                            <div class="three-width-col col-2 cols-4">
                                <?php while ( have_rows( "module_2" ) ) : the_row();
                                    get_layout_modules( get_row_layout() );
                                endwhile; ?>
                            </div>
                            <div class="three-width-col col-3 cols-4 last">
                                <?php while ( have_rows( "module_3" ) ) : the_row();
                                    get_layout_modules( get_row_layout() );
                                endwhile; ?>
                            </div>

                        <?php elseif ( get_row_layout() == 'four_width_cols' ) : ?>

                            <div class="four-width-col col-1 cols-3">
                                <?php while ( have_rows( "module_1" ) ) : the_row();
                                    get_layout_modules( get_row_layout() );
                                endwhile; ?>
                            </div>
                            <div class="four-width-col col-2 cols-3">
                                <?php while ( have_rows( "module_2" ) ) : the_row();
                                    get_layout_modules( get_row_layout() );
                                endwhile; ?>
                            </div>
                            <div class="four-width-col col-3 cols-3">
                                <?php while ( have_rows( "module_3" ) ) : the_row();
                                    get_layout_modules( get_row_layout() );
                                endwhile; ?>
                            </div>
                            <div class="four-width-col col-4 cols-3 last">
                                <?php while ( have_rows( "module_4" ) ) : the_row();
                                    get_layout_modules( get_row_layout() );
                                endwhile; ?>
                            </div>

                        <?php elseif ( get_row_layout() == 'five_width_cols' ) : ?>

                            <div class="five-width-col col-1 cols-2">
                                <?php while ( have_rows( "module_1" ) ) : the_row();
                                    get_layout_modules( get_row_layout() );
                                endwhile; ?>
                            </div>
                            <div class="five-width-col  col-2 cols-2">
                                <?php while ( have_rows( "module_2" ) ) : the_row();
                                    get_layout_modules( get_row_layout() );
                                endwhile; ?>
                            </div>
                            <div class="five-width-col col-3 cols-2">
                                <?php while ( have_rows( "module_3" ) ) : the_row();
                                    get_layout_modules( get_row_layout() );
                                endwhile; ?>
                            </div>
                            <div class="five-width-col col-4 cols-2">
                                <?php while ( have_rows( "module_4" ) ) : the_row();
                                    get_layout_modules( get_row_layout() );
                                endwhile; ?>
                            </div>
                            <div class="five-width-col col-5 cols-2 last">
                                <?php while ( have_rows( "module_5" ) ) : the_row();
                                    get_layout_modules( get_row_layout() );
                                endwhile; ?>
                            </div>

                        <?php endif;
                    endwhile; ?>

                </div> <!--end section-content-->
            </section>
        <?php endwhile;
    else :
        // no layouts found
    endif;
}

function layout_module_slider() {
    ob_start();

    ?><div class="layout-module-slider <?= get_sub_field('class'); ?>">
        <?php
        $files = get_sub_field('img_slider');
        // pre is a function which wraps a value with print_r
        pre($files); // <-- prints the "empty" array
        if(count($files) > 0 ) {
            echo slick_slider_func($files);
        } else {
            echo "No images available";
        }
        ?>
    </div><?php

    return ob_get_clean();
}

原文:

我在灵活的内容层中有一个嵌套的转发器字段。其目的是添加输出到滑块的图像。

图像 ID 存储在数据库中,但是当我尝试使用 get_sub_field 输出它们时,我得到的是:

我以编程方式构建了字段组,它在后端工作,如下所示。上述输出的后端等效项:

整个 ACF 设置/程序:

if ( function_exists( 'acf_add_local_field_group' ) ):

    add_action("init", "tryangle_generate_sections_fields");
    function tryangle_generate_sections_fields() {
        $indhold_gd = array(
            'key' => 'group_indhold',
            'title' => 'Indhold',
            'fields' => array (),
            'location' => array (
                array (
                    array (
                        'param' => 'post_type',
                        'operator' => '==',
                        'value' => 'page',
                    ),
                ),
            ),
            'position' => 'acf_after_title',
            'menu_order' => 0,
        );
        generate_fields( "indhold224466", $indhold_gd );
    }

endif;

function generate_fields($uniqid, $group_data)
{
    if(empty($uniqid) || empty($group_data)) return;

    // Sets a unique affix on all keys
    $uiq = $uniqid;
    // Sets the group_data with locations etc.
    $group_data['key'] = $group_data['key'] . '_' . $uiq;

    // Available modules
    $modules = array(
        array(
            'key'        => '5b2cefcfdea9da_'.$uiq,
            'name'       => 'text',
            'label'      => 'Text',
            'display'    => 'block',
            'sub_fields' => array(
                array(
                    'key'               => 'field_5b2d13ddfb10ba_'.$uiq,
                    'label'             => 'Text',
                    'name'              => 'text',
                    'type'              => 'wysiwyg',
                    'instructions'      => '',
                    'required'          => 0,
                    'conditional_logic' => 0,
                    'wrapper'           => array(
                        'width' => '',
                        'class' => '',
                        'id'    => '',
                    ),
                    'default_value'     => '',
                    'tabs'              => 'all',
                    'toolbar'           => 'full',
                    'media_upload'      => 1,
                    'delay'             => 0,
                ),
            ),
            'min'        => '',
            'max'        => '',
        ),
        array (
            'key' => '5b3e2a167d2ee_'.$uiq,
            'name' => 'image_slider',
            'label' => 'Slider',
            'display' => 'block',
            'sub_fields' => array (
                array (
                    'key' => 'field_5b3e2a3047a45_'.$uiq,
                    'label' => 'Slider',
                    'name' => 'img_slider',
                    'type' => 'repeater',
                    'instructions' => '',
                    'required' => 0,
                    'conditional_logic' => 0,
                    'wrapper' => array (
                        'width' => '',
                        'class' => '',
                        'id' => '',
                    ),
                    'collapsed' => '',
                    'min' => 0,
                    'max' => 0,
                    'layout' => 'table',
                    'button_label' => 'Tilføj billede',
                    'sub_fields' => array (
                        array (
                            'key' => 'field_5b3e2a3b47a46_'.$uiq,
                            'label' => 'Billede',
                            'name' => 'img',
                            'type' => 'image',
                            'instructions' => '',
                            'required' => 0,
                            'conditional_logic' => 0,
                            'wrapper' => array (
                                'width' => '20',
                                'class' => '',
                                'id' => '',
                            ),
                            'return_format' => 'array',
                            'preview_size' => 'thumbnail',
                            'library' => 'all',
                            'min_width' => '',
                            'min_height' => '',
                            'min_size' => '',
                            'max_width' => '',
                            'max_height' => '',
                            'max_size' => '',
                            'mime_types' => '',
                        ),
                        array (
                            'key' => 'field_5b3e2a5647a47_'.$uiq,
                            'label' => 'Billedetekst',
                            'name' => 'img_text',
                            'type' => 'wysiwyg',
                            'instructions' => '',
                            'required' => 0,
                            'conditional_logic' => 0,
                            'wrapper' => array (
                                'width' => '',
                                'class' => '',
                                'id' => '',
                            ),
                            'default_value' => '',
                            'tabs' => 'all',
                            'toolbar' => 'full',
                            'media_upload' => 1,
                            'delay' => 0,
                        ),
                    ),
                ),
            ),
            'min' => '',
            'max' => '',
        ),
        array(
            'key'        => '5b2cf0bbbf1c9a_'.$uiq,
            'name'       => 'posts_slider',
            'label'      => 'Indlægsslider',
            'display'    => 'block',
            'sub_fields' => array(
                array(
                    'key'               => 'field_5b2d13ddfb110a_'.$uiq,
                    'label'             => 'Indlægskategori',
                    'name'              => 'post_category',
                    'type'              => 'taxonomy',
                    'instructions'      => '',
                    'required'          => 0,
                    'conditional_logic' => 0,
                    'wrapper'           => array(
                        'width' => '',
                        'class' => '',
                        'id'    => '',
                    ),
                    'taxonomy'          => 'category',
                    'field_type'        => 'select',
                    'allow_null'        => 0,
                    'add_term'          => 0,
                    'save_terms'        => 0,
                    'load_terms'        => 0,
                    'return_format'     => 'object',
                    'multiple'          => 0,
                ),
                array(
                    'key'               => 'field_5b2d13ddfb111a_'.$uiq,
                    'label'             => 'Udseende',
                    'name'              => 'style',
                    'type'              => 'select',
                    'instructions'      => '',
                    'required'          => 0,
                    'conditional_logic' => 0,
                    'wrapper'           => array(
                        'width' => '',
                        'class' => '',
                        'id'    => '',
                    ),
                    'choices'           => array(
                        'Slider' => 'Slider',
                        'Grid'   => 'Grid',
                    ),
                    'default_value'     => array(),
                    'allow_null'        => 0,
                    'multiple'          => 0,
                    'ui'                => 0,
                    'ajax'              => 0,
                    'return_format'     => 'value',
                    'placeholder'       => '',
                ),
            ),
            'min'        => '',
            'max'        => '',
        ),
    );
    // Section columns
    $columns = array(
        array(
            'key'        => 'field_one_width_cols_'.$uiq,
            'name'       => 'one_width_cols',
            'label'      => 'En kolonne',
            'display'    => 'block',
            'sub_fields' => array(
            ),
            'min'        => '',
            'max'        => '',
            'cols'       => 1,
        ),
        array(
            'key'        => 'field_two_width_cols_'.$uiq,
            'name'       => 'two_width_cols',
            'label'      => 'To kolonner',
            'display'    => 'block',
            'sub_fields' => array(),
            'min'        => '',
            'max'        => '',
            'cols'       => 2,
        ),
        array(
            'key'        => 'field_three_width_cols_'.$uiq,
            'name'       => 'three_width_cols',
            'label'      => 'Tre kolonner',
            'display'    => 'block',
            'sub_fields' => array(),
            'min'        => '',
            'max'        => '',
            'cols'       => 3,
        ),
        array(
            'key'        => 'field_four_width_cols_'.$uiq,
            'name'       => 'four_width_cols',
            'label'      => 'Fire kolonner',
            'display'    => 'block',
            'sub_fields' => array(),
            'min'        => '',
            'max'        => '',
            'cols'       => 4,
        ),
        array(
            'key'        => 'field_five_width_cols_'.$uiq,
            'name'       => 'five_width_cols',
            'label'      => 'Fem kolonner',
            'display'    => 'block',
            'sub_fields' => array(),
            'min'        => '',
            'max'        => '',
            'cols'       => 5,
        )
    );

    /**
     * Add columns to sections
     */
    $loops = 1;
    foreach ($columns as $key => $col)
    {
        // loop for the amount of columns available (5 columns)
        for ($i = 1; $i <= $col['cols']; $i++) {
            $width = 100 / $loops;
            $columns[$key]['sub_fields'][] = array(
                'key'               => 'field_module_'.$i.'_'.$uiq,
                'label'             => 'Modul',
                'name'              => 'module_'.$i,
                'type'              => 'flexible_content',
                'instructions'      => '',
                'required'          => 0,
                'conditional_logic' => 0,
                'wrapper'           => array(
                    'width' => $width,
                    'class' => '',
                    'id'    => '',
                ),
                'min'               => '',
                'max'               => '',
                'button_label'      => 'Tilføj modul',
                'layouts'           => $modules,
            );
        }

        $loops++;
    }

    // Add field group "Indhold"
    acf_add_local_field_group($group_data);

    // Add sections to the "Indhold" field group
    acf_add_local_field(array(
        'parent'        => 'group_indhold_'.$uiq,
        'key'           => 'field_sections_'.$uiq,
        'label'         => 'Sektioner',
        'name'          => 'sections',
        'type'          => 'flexible_content',
        'button_label'  => 'Tilføj sektion',
        'layouts' => array(
            array(
                'key'        => 'layout_section_'.$uiq,
                'name'       => 'section',
                'label'      => 'Sektion',
                'display'    => 'block',
                'sub_fields' => array(
                    array(
                        'key'               => 'field_section_'.$uiq,
                        'label'             => 'Sektion',
                        'name'              => 'section',
                        'type'              => 'flexible_content',
                        'instructions'      => '',
                        'required'          => 0,
                        'conditional_logic' => 0,
                        'wrapper'           => array(
                            'width' => '',
                            'class' => '',
                            'id'    => '',
                        ),
                        'min'               => '',
                        'max'               => '',
                        'button_label'      => 'Tilføj kolonner',
                        'layouts'           => $columns,
                    ),
                    array(
                        'key'               => 'field_section_class_'.$uiq,
                        'label'             => 'Sektion class',
                        'name'              => 'class',
                        'type'              => 'text',
                        'instructions'      => '',
                        'required'          => 0,
                        'conditional_logic' => 0,
                        'wrapper'           => array(
                            'width' => '',
                            'class' => '',
                            'id'    => '',
                        ),
                        'default_value'     => '',
                        'placeholder'       => '',
                        'prepend'           => '',
                        'append'            => '',
                        'maxlength'         => '',
                    ),
                ),
            ),
        ),
    ));
}

非常感谢您的帮助!我一整天都被这个问题困扰着。 谢谢!

【问题讨论】:

    标签: wordpress advanced-custom-fields


    【解决方案1】:

    好吧,我不知道为什么它是空的:

    pre($files); // <-- prints the "empty" array
    

    但是,您可以替换它:

    $files = get_sub_field('img_slider');
    

    ..with this: (手动为$files构建数据)

    $files = array();
    if ( have_rows( 'img_slider' ) ) {
        // Loop through the repeater's fields and manually fill in the `$files` array.
        while ( have_rows( 'img_slider' ) ) : the_row();
            if ( $image = get_sub_field( 'img' ) ) {
                // These are just examples; just rename the keys, add more items, etc.
                // it's all up to you.
                $files[] = array(
                    'url'  => $image['url'],
                    'text' => get_sub_field( 'img_text' ),
                    // ...
                );
            }
        endwhile;
    }
    //var_dump( $files );
    

    附加代码

    这个循环也可以帮助你,我曾经从最顶层的 sections 字段开始访问滑块项目(即图像及其标题/文本):

    $post_id = false; // `false` defaults to the global `$post`
    if ( have_rows( 'sections', $post_id ) ) :
        while ( have_rows( 'sections', $post_id ) ) : the_row(); // loop #1
            // With the current fields defined in `generate_fields()`, the 'class'
            // is only available in loop #1.
            $section_class = get_sub_field( 'class' );
    
            echo '<div class="' . esc_attr( $section_class ) . '">'; // start the section
    
            if ( have_rows( 'section' ) ) {
                while ( have_rows( 'section' ) ) : the_row(); // loop #2
                    if ( have_rows( 'module_1' ) ) {
                        while ( have_rows( 'module_1' ) ) : the_row(); // loop #3
                            // In section #2 only.
                            the_sub_field( 'text' );
    
                            // In section #1 only.
                            if ( have_rows( 'img_slider' ) ) {
                                echo '<ul class="slider">';
    
                                while ( have_rows( 'img_slider' ) ) : the_row(); // loop #4
                                    echo '<li class="slide-item">';
    
                                    $image = get_sub_field( 'img' );
                                    echo wp_get_attachment_image( $image['id'] );
    
                                    the_sub_field( 'img_text' );
    
                                    echo '</li>';
                                endwhile; // end loop #4
    
                                echo '</ul>';
                            }
                        endwhile; // end loop #3
                    }
                endwhile; // end loop #2
            }
    
            echo '</div>'; // end section
        endwhile; // end loop #1
    endif;
    

    第 1 节 (image-slider)

    第 2 节 (lorem-ipsum)

    【讨论】:

    • 嗨,莎莉,感谢您再次回答。我不确定这是否会。从那以后,我在这个程序上从头开始,并重新编写了 ACF 中的所有字段,同时在新程序中仔细地为所有字段生成唯一键(键可能与它有关)。谢天谢地,更新后的代码现在可以工作了。
    • 没问题。实际上我确实看到了重复项,但我不知道为什么我没有提及它们,甚至没有尝试使它们独一无二!我想我以为你也已经尝试过了。 xD 顺便说一句,我删除了我之前的答案.. 但感谢您的投票!
    【解决方案2】:

    我从头开始,重新编写了 ACF 中的所有字段,并使用唯一生成的密钥以编程方式小心地添加了这些字段(某些密钥重复可能是问题所在)。谢天谢地,更新后的代码现在可以工作了。

    下面发布的完整代码(没有输出,应该与标准 ACF 文档一起使用)。 我还添加了用唯一键替换字段键的爬虫。

    不确定原始帖子的错误在哪里,尽管这个新代码可以工作并且完全一样。

    <?php
    
    if ( function_exists( 'acf_add_local_field_group' ) ):
    
        add_action("init", "tryangle_generate_sections_fields");
        function tryangle_generate_sections_fields() {
            $indhold_gd = array(
                'key' => 'group_5b3f61bd82eba',
                'title' => 'Indhold',
                'location' => array (
                    array (
                        array (
                            'param' => 'post_type',
                            'operator' => '==',
                            'value' => 'page',
                        ),
                    ),
                ),
                'position' => 'acf_after_title',
                'menu_order' => 0,
                'style' => 'default',
                'label_placement' => 'top',
                'instruction_placement' => 'label',
                'hide_on_screen' => '',
                'active' => 1,
                'description' => '',
            );
            generate_fields( "allpages", $indhold_gd );
        }
    
    
    endif;
    
    /**
     * Alters the ACF keys of the passed array, with the pass key affix
     * @param $fields array Take ACF fields array as input.
     * @param $randkey string String which is appended on the key
     *
     * @return array Returns passed array with altered keys
     */
    function crawl_generate_keys($fields, $randkey) {
        if(is_array($fields))
        {
            foreach ( $fields as $fk => $f )
            {
                if(!empty(is_array($fields[$fk]['sub_fields'])))
                {
                    $fields[$fk]['sub_fields'] = crawl_generate_keys($fields[$fk]['sub_fields'], $randkey);
                }
                else if(!empty(is_array($fields[$fk]['layouts'])))
                {
                    $fields[$fk]['layouts'] = crawl_generate_keys($fields[$fk]['layouts'], $randkey);
                }
    
                $fields[$fk]['key'] = $fields[$fk]['key'] . '_' . $randkey;
            }
        } else {
            $fields['key'] = $fields['key'] . '_' . $randkey;
        }
    
        return $fields;
    }
    
    function generate_fields($uniqid, $gd)
    {
        // Declare the available layout modules
        // The different layouts, such as slider, text field, statement slider
        $layouts = array(
            array (
                'key' => '5b3f63d3bc38c_' . $uniqid,
                'name' => 'text',
                'label' => 'Text',
                'display' => 'block',
                'sub_fields' => array (
                    array (
                        'key' => 'field_5b3f641bb0588_' . $uniqid,
                        'label' => 'Text',
                        'name' => 'text',
                        'type' => 'wysiwyg',
                        'instructions' => '',
                        'required' => 0,
                        'conditional_logic' => 0,
                        'wrapper' => array (
                            'width' => '',
                            'class' => '',
                            'id' => '',
                        ),
                        'default_value' => '',
                        'tabs' => 'all',
                        'toolbar' => 'full',
                        'media_upload' => 1,
                        'delay' => 0,
                    ),
                ),
                'min' => '',
                'max' => '',
            ),
            array (
                'key' => '5b3f6425b0589_' . $uniqid,
                'name' => 'slider',
                'label' => 'Slider',
                'display' => 'block',
                'sub_fields' => array (
                    array (
                        'key' => 'field_5b3f6428b058a_' . $uniqid,
                        'label' => 'Slider',
                        'name' => 'slider',
                        'type' => 'repeater',
                        'instructions' => '',
                        'required' => 0,
                        'conditional_logic' => 0,
                        'wrapper' => array (
                            'width' => '',
                            'class' => '',
                            'id' => '',
                        ),
                        'collapsed' => '',
                        'min' => 0,
                        'max' => 0,
                        'layout' => 'table',
                        'button_label' => 'Tilføj billede',
                        'sub_fields' => array (
                            array (
                                'key' => 'field_5b3f642fb058b_' . $uniqid,
                                'label' => 'Billede',
                                'name' => 'image',
                                'type' => 'image',
                                'instructions' => '',
                                'required' => 0,
                                'conditional_logic' => 0,
                                'wrapper' => array (
                                    'width' => '20',
                                    'class' => '',
                                    'id' => '',
                                ),
                                'return_format' => 'array',
                                'preview_size' => 'thumbnail',
                                'library' => 'all',
                                'min_width' => '',
                                'min_height' => '',
                                'min_size' => '',
                                'max_width' => '',
                                'max_height' => '',
                                'max_size' => '',
                                'mime_types' => '',
                            ),
                            array (
                                'key' => 'field_5b3f6453b058c_' . $uniqid,
                                'label' => 'Billede tekst',
                                'name' => 'image_text',
                                'type' => 'wysiwyg',
                                'instructions' => '',
                                'required' => 0,
                                'conditional_logic' => 0,
                                'wrapper' => array (
                                    'width' => '',
                                    'class' => '',
                                    'id' => '',
                                ),
                                'default_value' => '',
                                'tabs' => 'all',
                                'toolbar' => 'full',
                                'media_upload' => 1,
                                'delay' => 0,
                            ),
                        ),
                    ),
                    array (
                        'key' => 'field_5b40ec12be700_' . $uniqid,
                        'label' => 'Slider class',
                        'name' => 'slider_class',
                        'type' => 'text',
                        'instructions' => '',
                        'required' => 0,
                        'conditional_logic' => 0,
                        'wrapper' => array (
                            'width' => '',
                            'class' => '',
                            'id' => '',
                        ),
                        'default_value' => '',
                        'placeholder' => '',
                        'prepend' => '',
                        'append' => '',
                        'maxlength' => '',
                    ),
    
                ),
                'min' => '',
                'max' => '',
            ),
            array (
                'key' => '5b3f64a5b058d_' . $uniqid,
                'name' => 'statements',
                'label' => 'Udtalelser',
                'display' => 'block',
                'sub_fields' => array (
                    array (
                        'key' => 'field_5b3f64b4b058e_' . $uniqid,
                        'label' => 'Udtalelser',
                        'name' => 'statements',
                        'type' => 'post_object',
                        'instructions' => '',
                        'required' => 0,
                        'conditional_logic' => 0,
                        'wrapper' => array (
                            'width' => '',
                            'class' => '',
                            'id' => '',
                        ),
                        'post_type' => array (
                            0 => 'udtalelser',
                        ),
                        'taxonomy' => array (
                        ),
                        'allow_null' => 0,
                        'multiple' => 1,
                        'return_format' => 'object',
                        'ui' => 1,
                    ),
                ),
                'min' => '',
                'max' => '',
            ),
        );
    
        // Declare the avaialable columns
        // The key indicates amount columns for that column
        $columns = array (
            1 => array (
                'key' => '5b3f636009d7e1_' . $uniqid,
                'name' => 'one_width_cols',
                'label' => 'En kolonne',
                'display' => 'block',
                'sub_fields' => array(),
                'min' => '',
                'max' => '',
            ),
            2 => array (
                'key' => '5b3f636009d7e2_' . $uniqid,
                'name' => 'two_width_cols',
                'label' => 'To kolonner',
                'display' => 'block',
                'sub_fields' => array(),
                'min' => '',
                'max' => '',
            ),
            3 => array (
                'key' => '5b3f636009d7e3_' . $uniqid,
                'name' => 'three_width_cols',
                'label' => 'Tre kolonner',
                'display' => 'block',
                'sub_fields' => array(),
                'min' => '',
                'max' => '',
            ),
            4 => array (
                'key' => '5b3f636009d7e4_' . $uniqid,
                'name' => 'four_width_cols',
                'label' => 'Fire kolonner',
                'display' => 'block',
                'sub_fields' => array(),
                'min' => '',
                'max' => '',
            ),
            5 => array (
                'key' => '5b3f636009d7e5_' . $uniqid,
                'name' => 'five_width_cols',
                'label' => 'Fem kolonner',
                'display' => 'block',
                'sub_fields' => array(),
                'min' => '',
                'max' => '',
            ),
        );
    
        // Add layout modules to modules, add modules to columns
        foreach ($columns as $k => $c)
        {
            // Loop for every module to be added to the column
            for ($i = 1; $i <= $k; $i++)
            {
                $keyaffix = 'c' . $k . '_m' . $i;
                $wrapperWidth = 100 / $k;
    
                $insLayouts = crawl_generate_keys($layouts, $keyaffix);
    
                $columns[$k]['sub_fields'][] = array(
                    'key' => 'field_5b3f63c1b0586_' . $keyaffix . '_' . $uniqid,
                    'label' => 'Modul',
                    'name' => 'module_' . $i,
                    'type' => 'flexible_content',
                    'instructions' => '',
                    'required' => 0,
                    'conditional_logic' => 0,
                    'wrapper' => array (
                        'width' => $wrapperWidth,
                        'class' => '',
                        'id' => '',
                    ),
                    'button_label' => 'Tilføj modul',
                    'min' => '',
                    'max' => '',
                    'layouts' => $insLayouts,
                );
            }
        }
    
        acf_add_local_field_group(array (
            'key' => $gd['key'] . '_'. $uniqid,
            'title' => $gd['title'],
            'fields' => array (
                array (
                    'key' => 'field_5b3f61c74f5b2_' . $uniqid,
                    'label' => 'Sektioner',
                    'name' => 'sections',
                    'type' => 'flexible_content',
                    'instructions' => '',
                    'required' => 0,
                    'conditional_logic' => 0,
                    'wrapper' => array (
                        'width' => '',
                        'class' => '',
                        'id' => '',
                    ),
                    'button_label' => 'Tilføj sektion',
                    'min' => '',
                    'max' => '',
                    'layouts' => array (
                        array (
                            'key' => '5b3f61ce24ec5_' . $uniqid,
                            'name' => 'section',
                            'label' => 'Sektion',
                            'display' => 'block',
                            'sub_fields' => array (
                                array (
                                    'key' => 'field_5b3f6359b0585_' . $uniqid,
                                    'label' => 'Sektion',
                                    'name' => 'section',
                                    'type' => 'flexible_content',
                                    'instructions' => '',
                                    'required' => 0,
                                    'conditional_logic' => 0,
                                    'wrapper' => array (
                                        'width' => '',
                                        'class' => '',
                                        'id' => '',
                                    ),
                                    'button_label' => 'Tilføj kolonner',
                                    'min' => '',
                                    'max' => '',
                                    'layouts' => $columns,
                                ),
                            ),
                            'min' => '',
                            'max' => '',
                        ),
                    ),
                ),
            ),
            'location' => $gd['location'],
            'menu_order' => $gd['menu_order'],
            'position' => $gd['position'],
            'style' => $gd['style'],
            'label_placement' => $gd['label_placement'],
            'instruction_placement' => $gd['instruction_placement'],
            'hide_on_screen' => $gd['hide_on_screen'],
            'active' => $gd['active'],
            'description' => $gd['description'],
        ));
    }
    

    【讨论】:

    • 我投了赞成票,因为这个答案和问题对我都有帮助。谢谢,我很高兴你找到了解决方案! =)
    • 嗨,莎莉非常感谢您,感谢您的帮助和尝试,非常感谢。很高兴我能帮助你! :-)
    猜你喜欢
    • 1970-01-01
    • 2022-11-20
    • 1970-01-01
    • 1970-01-01
    • 2011-10-02
    • 2019-09-08
    • 1970-01-01
    • 1970-01-01
    • 2022-12-04
    相关资源
    最近更新 更多