使用高级自定义字段“灵活内容”和自定义帖子

时间:2015-04-28 17:00:24

标签: php wordpress wordpress-plugin custom-post-type advanced-custom-fields

我希望有人可以帮助我发现我的错误,因为我浪费了无数个小时。我使用具有灵活内容的高级自定义字段插件模板。我正在使用灵活的内容字段,其中包含我目前正在处理的网站上的大量模板。我使用了相同的策略,我总是用它来显示我的自定义帖子类型'学位和程序'中的所有帖子的可选自定义字段。我的灵活内容适用于我的所有页面,但我的单个自定义帖子的页面除外。我有一个页面'single-degrees-and-programs.php',我有:

<?php while (has_sub_field("program_content")): ?>

在我的functions.php中,我已经在我的灵活内容中设置了所有字段,如下所示(希望这里有一个错误,我很遗憾,但我对此表示怀疑,因为所有灵活的字段都显示在我的自定义管理员中讯息。

register_field_group([
    'id' => 'acf_program',
    'title' => 'Program',
    'fields' => [
        [
            'key' => 'pc000',
            'label' => 'Degrees Offered',
            'name' => 'degrees_offered',
            'type' => 'text',
            'instructions' => 'Add the degrees offered',
            'default_value' => '',
            'placeholder' => '',
            'prepend' => '',
            'append' => '',
            'formatting' => 'html',
            'maxlength' => '',
        ],
        [
            'key' => 'pc0',
            'label' => 'Program Content',
            'name' => 'program_content',
            'type' => 'flexible_content',
            'layouts' => [
                [
                    'label' => 'Intro',
                    'name' => 'intro',
                    'display' => 'row',
                    'min' => '',
                    'max' => '',
                    'sub_fields' => [
                        [
                            'key' => 'pc1',
                            'label' => 'Heading',
                            'name' => 'heading',
                            'type' => 'text',
                            'column_width' => '',
                            'default_value' => '',
                            'placeholder' => '',
                            'prepend' => '',
                            'append' => '',
                            'formatting' => 'html',
                            'maxlength' => '',
                        ],
                        [
                            'key' => 'pc2',
                            'label' => 'Intro Text',
                            'name' => 'intro_text',
                            'type' => 'textarea',
                            'column_width' => '',
                            'default_value' => '',
                            'placeholder' => '',
                            'maxlength' => '',
                            'rows' => '',
                            'formatting' => 'br',
                        ],
                    ],
                ],
                [
                    'label' => 'Inline Image',
                    'name' => 'inline_image',
                    'display' => 'row',
                    'min' => '',
                    'max' => '',
                    'sub_fields' => [
                        [
                            'key' => 'pc3',
                            'label' => 'Image',
                            'name' => 'image',
                            'type' => 'image',
                            'column_width' => '',
                            'save_format' => 'object',
                            'preview_size' => 'full',
                            'library' => 'all',
                        ],
                        [
                            'key' => 'pc4',
                            'label' => 'Caption',
                            'name' => 'caption',
                            'type' => 'textarea',
                            'column_width' => '',
                            'default_value' => '',
                            'placeholder' => '',
                            'maxlength' => '',
                            'rows' => '',
                            'formatting' => 'br',
                        ],
                    ],
                ],
                [
                    'label' => 'CTA block',
                    'name' => 'cta_block',
                    'display' => 'row',
                    'min' => '',
                    'max' => '',
                    'sub_fields' => [
                        [
                            'key' => 'pc5',
                            'label' => 'Block heading',
                            'name' => 'block_heading',
                            'type' => 'text',
                            'column_width' => '',
                            'default_value' => '',
                            'placeholder' => '',
                            'prepend' => '',
                            'append' => '',
                            'formatting' => 'html',
                            'maxlength' => '',
                        ],
                        [
                            'key' => 'pc6',
                            'label' => 'Block Text',
                            'name' => 'block_text',
                            'type' => 'textarea',
                            'column_width' => '',
                            'default_value' => '',
                            'placeholder' => '',
                            'maxlength' => '',
                            'rows' => '',
                            'formatting' => 'br',
                        ],
                        [
                            'key' => 'pc7',
                            'label' => 'Block Button',
                            'name' => 'block_button',
                            'type' => 'text',
                            'column_width' => '',
                            'default_value' => '',
                            'placeholder' => '',
                            'prepend' => '',
                            'append' => '',
                            'formatting' => 'html',
                            'maxlength' => '',
                        ],
                    ],
                ],
                [
                    'label' => 'Italic Slab',
                    'name' => 'italic_slab',
                    'display' => 'row',
                    'min' => '',
                    'max' => '',
                    'sub_fields' => [
                        [
                            'key' => 'pc8',
                            'label' => 'paragraph',
                            'name' => 'paragraph',
                            'type' => 'textarea',
                            'column_width' => '',
                            'default_value' => '',
                            'placeholder' => '',
                            'maxlength' => '',
                            'rows' => '',
                            'formatting' => 'br',
                        ],
                    ],
                ],
                [
                    'label' => 'Zip Down Menu',
                    'name' => 'zip_down_menu',
                    'display' => 'row',
                    'min' => '',
                    'max' => '',
                    'sub_fields' => [
                        [
                            'key' => 'pc9',
                            'label' => 'Zip Section',
                            'name' => 'zip_section',
                            'type' => 'repeater',
                            'column_width' => '',
                            'sub_fields' => [
                                [
                                    'key' => 'pc10',
                                    'label' => 'Title',
                                    'name' => 'title',
                                    'type' => 'text',
                                    'column_width' => '',
                                    'default_value' => '',
                                    'placeholder' => '',
                                    'prepend' => '',
                                    'append' => '',
                                    'formatting' => 'html',
                                    'maxlength' => '',
                                ],
                                [
                                    'key' => 'pc11',
                                    'label' => 'content',
                                    'name' => 'content',
                                    'type' => 'wysiwyg',
                                    'column_width' => '',
                                    'default_value' => '',
                                    'toolbar' => 'full',
                                    'media_upload' => 'yes',
                                ],
                            ],
                            'row_min' => '',
                            'row_limit' => '',
                            'layout' => 'row',
                            'button_label' => 'Add Row',
                        ],
                    ],
                ],

            ],
            'button_label' => 'Add Row',
            'min' => '',
            'max' => '',
        ],
    ],
    'location' => [
        [
            [
                'param' => 'post_type',
                'operator' => '==',
                'value' => 'degrees-and-programs',
                'order_no' => 0,
                'group_no' => 0,
            ],
        ],
    ],
    'options' => [
        'position' => 'normal',
        'layout' => 'no_box',
        'hide_on_screen' => [
        ],
    ],
    'menu_order' => 0,
]);

有没有人知道为什么这不起作用。我真的需要帮助。

1 个答案:

答案 0 :(得分:0)

我认为你需要:

if( have_rows('program_content') ): // Check the Flexible Content has rows

  while ( have_rows('program_content') ) : the_row();

    if( get_row_layout() == 'intro' ): // Check the type of each row
      the_sub_field('intro_text'); // Do something with it
    elseif( get_row_layout() == 'inline_image' ):
      ...
    endif;

  endwhile;

endif;

使用该格式,让我知道你是如何进行的:)