Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Leanne D
Leanne D
4,550 Points

WordPress ACF Flexible Content - Using a layout block more than once on one page duplicates content

I have a ‘flexible-content’ field called coaching_flex_sections. In here I have a ‘layout’ called flex_coaching_details.

I want to be able to use that layout multiple times in different places on one page. So in the page editor, I added this layout block 6 times.

It is outputting the 6 blocks, but reapeated six times, like so:

Title One Title One Title One Title One Title One Title One

Title Two Title Two Title Two Title Two Title Two Title Two

and so on down to Title Six…

Can someone explain to me where I have gone wrong as I can’t quite get my head around this. I THINK it has something to do with my loop, but whatever I try is not working.

Flex Layout Page Template Loop - here I pull all the layout files that are saved in their own .php files:

<?php /* Template Name: Coaching (Flex Layout) */

get_header(); ?>

<?php if( have_rows('coaching_flex_sections') ): $sections = get_field( 'coaching_flex_sections' ); ?>

<div class="coaching flex-sections">
    <?php
        while( have_rows('coaching_flex_sections') ): the_row();
            // while( have_posts() ): the_post();

                // Loop through flexible fields and load the respective file for each.
                foreach( $sections as $i => $section ) {
                    $part = get_stylesheet_directory() . '/_template-parts/flex-layout/sections/' . $section['acf_fc_layout'] . '.php';

                    if ( file_exists( $part ) ) {
                        include( $part );
                    } else {
                        echo '<!-- Error: No such flexible field type "'. esc_html($section['acf_fc_layout']) .' at '. esc_html($part) .'" -->';
                    }

                }
            // endwhile;
        endwhile;
    ?>
</div>

<?php endif; ?>

Layout template file example: <?php if ( !isset($section) ) die('Must be accessed through flex-layout.php');

// Fields used by this section with default values
$f = shortcode_atts(array(
    'content_appearance' => null,
    'content' => null,
), $section, 'custom-layout');

// Extra classes to use on the container
$classes = array();
$classes[] = 'appearance-' . $f['content_appearance'];

// Render the section
aa_flex_layout_section_start($section, $i, $classes);

?>

<?php if( get_row_layout() == 'flex_coaching_details' ): ?>

    <!-- flex section content start -->
    <?php $coaching_details_subheading = get_sub_field('flex_coaching_details_subheading'); ?>
    <?= ($coaching_details_subheading)? '<h2 class="heading">' . $coaching_details_subheading . '</h2>' : ''; ?>
    <!-- flex section content end -->
<?php endif; ?>

<?php aa_flex_layout_section_end();

Many thanks