2
votes

I am hoping someone can help me spot my mistake because I have wasted countless hours on this one. I use the Advanced Custom Fields plugin templates with flexible content. I am using the flexible content field with numerous templates on a site that I am currently working on. I have used the same strategy that I always use to display optional custom fields for all posts within my Custom Post Type 'Degrees and programs'. My flexible content is working on all of my pages except the pages for my single custom posts. I have a page 'single-degrees-and-programs.php' in which I have:

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

In my functions.php I have set up all of the fields within my flexible content as follows (hopefully there's an error in here Im missing, but I doubt it because all of the flexible fields show up in the admin for my custom posts.

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

Does anyone have any input on why this isn't working. I really need help on this one.

1

1 Answers

0
votes

I think you need:

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;

Have a go with that format and let me know how you get on :)