I had a similar issue, with Flexible Content fields that I needed to display in a 'modular' style page - eg the page content has a wrapper around content sections, but the Call To Action and Testimonial Slider need to close that wrapper before it starts and open it afterwards.. so only the Call To Action and Testimonials are full width.
Here is what I came up with, although, there is likely a more elegant way to code this.. this works :)
<?php
$module_open; // set var
$module_close; // set var
function content_module_open(){
global $module_open;
global $module_close;
if($module_open !== TRUE){
echo '<div class="content-module">';
$module_open = true;
$module_close = false;
}
}
function content_module_close(){
global $module_open;
global $module_close;
if($module_close !== TRUE){
echo '</div><!-- content-module -->';
$module_close = true;
$module_open = false;
}
}
?>
<?php if (have_rows('flexible_content_types')): ?>
<?php while (have_rows('flexible_content_types')): the_row(); ?>
<?php if (get_row_layout() == 'heading_h1'): ?>
<?php content_module_open();?>
<h1 class=""><?php the_sub_field('heading'); ?></h1>
<?php elseif (get_row_layout() == 'heading_h2'): ?>
<?php content_module_open();?>
<h2 class=""><?php the_sub_field('heading'); ?></h2>
<?php elseif (get_row_layout() == 'paragraph_content'): ?>
<?php content_module_open();?>
<?php the_sub_field('content'); ?>
<?php elseif (get_row_layout() == 'faq_accordion'): ?>
<?php content_module_open();?>
<?php if (have_rows('faqs')): ?>
<?php $count = 0 ?>
<div class="accordion" id="faqAccordion">
<?php while (have_rows('faqs')): the_row(); ?>
<?php $count++; ?>
<div class="card">
<div class="card-header" id="heading<?php echo $count; ?>">
<h2 class="clearfix mb-0">
<a class="btn btn-link" role="button" data-toggle="collapse" data-target="#collapse<?php echo $count; ?>" aria-expanded="<?php if ($count === 1){echo 'true';}else{echo 'false';} ?>" aria-controls="collapse<?php echo $count; ?>">
<?php the_sub_field('question');echo $count; ?>
<i class="fas fa-<?php if ($count === 1){echo 'plus';}else{echo 'minus';} ?>"></i>
</a>
</h2>
</div>
<div id="collapse<?php echo $count; ?>" class="collapse <?php if ($count === 1){echo 'show';} ?>" aria-labelledby="heading<?php echo $count; ?>" data-parent="#faqAccordion">
<div class="card-body"><?php the_sub_field('answer'); ?></div>
</div>
</div>
<?php endwhile; ?>
</div>
<?php endif; ?>
<?php elseif (get_row_layout() == 'timeline'): ?>
<?php content_module_open();?>
<?php if (have_rows('timeline_items')): ?>
<ul class="timeline">
<?php $time = 0 ?>
<?php while (have_rows('timeline_items')): the_row(); ?>
<?php $time++; ?>
<li class="<?php if ($time % 2 == 0){echo "timeline-inverted";} ?>">
<div class="timeline-date"><?php the_sub_field('year'); ?></div>
<div class="timeline-badge"><i class="fa fa-map-signs"></i></div>
<div class="timeline-panel">
<div class="timeline-heading">
<h4 class="timeline-title"><?php the_sub_field('title'); ?></h4>
</div>
<div class="timeline-body">
<p><?php the_sub_field('content'); ?></p>
</div>
</div>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
<?php elseif (get_row_layout() == 'association_list'): ?>
<?php content_module_open();?>
the_sub_field('associations');
the_sub_field('association_logo');
the_sub_field('assoc_title');
the_sub_field('assoc_description');
<?php elseif (get_row_layout() == 'call_to_action'): ?>
<?php content_module_close(); ?>
<?php if($module===0){echo $close;} ?>
<div class="content-cta cta-tabs cta-<?php the_sub_field('cta_style') ?> text-center">
<?php if (get_sub_field('cta_heading')): ?>
<h1 class=""><?php the_sub_field('cta_heading') ?></h1>
<?php endif; ?>
<?php if (get_sub_field('sub_text')): ?>
<p class="lead"><?php the_sub_field('sub_text') ?></p>
<?php endif; ?>
<?php if (get_sub_field('button_text') && get_sub_field('button_link')): ?>
<a href="<?php the_sub_field('button_link') ?>" class="btn btn-lg <?php the_sub_field('button_color') ?>"><?php the_sub_field('button_text') ?></a>
<?php endif; ?>
</div><!-- end div#content-cta -->
<?php elseif (get_row_layout() == 'testimonial_slider'): ?>
<?php content_module_close(); ?>
<?php if (have_rows('testimonials')): ?>
<div id="testimonial-slider" class="owl-carousel">
<?php while (have_rows('testimonials')): the_row(); ?>
<div class="testimonial">
<div class="testimonial-content">
<div class="testimonial-icon">
<i class="fas fa-quote-left"></i>
</div>
<p class="description"><?php the_sub_field('testimonial'); ?></p>
</div>
<h3 class="title"><?php the_sub_field('author'); ?></h3>
<span class="post"><?php the_sub_field('role'); ?></span>
</div>
<?php endwhile; ?>
</div>
<?php endif; //testimonials ?>
<?php endif; // get_row_layout ?>
<?php endwhile; // have_rows?>
<?php else: ?>
<div class="content-module">
<?php the_content(); ?>
</div><!-- end div.content-module -->
<?php endif; ?>
This will open the wrapper with the content_module_open() function, if it's not aleady open, for each element in the Flexible Content loop. And it closes it before the CTA and Testimonials..