Home › Forums › Add-ons › Flexible Content Field › Flexible content wrong order
Hello.
I’m trying to integrate three different layouts in a flexible content field on a page. I have a wysiwyg layout, an image layout and a text field layout.
But I can’t seem to get it right. After the loop is done I have this:
instead of:
What can be the issue?
Thanks!
What does your code look like that’s creating the incorrect order?
It looks like this:
<div>
<?php if( have_rows('post_content') ): ?>
<?php while ( have_rows('post_content') ) : the_row();
if( get_row_layout() == 'layout_text' ): ?>
<?php the_sub_field('text'); ?>
<?php endif; endwhile; ?>
<?php endif; ?>
</div>
<div>
<?php if( have_rows('post_content') ): ?>
<?php while ( have_rows('post_content') ) : the_row();
if( get_row_layout() == 'layout_image' ): ?>
<?php the_sub_field('image'); ?>
<?php endif; endwhile; ?>
<?php endif; ?>
</div>
<div>
<?php if( have_rows('post_content') ): ?>
<?php while ( have_rows('post_content') ) : the_row();
if( get_row_layout() == 'layout_video' ): ?>
<div class="videowrapper">
<?php the_sub_field('video'); ?>
</div>
<?php endif; endwhile; ?>
<?php endif; ?>
</div>
Maybe I should somehow make it in to one loop, but I can’t figure it out.
Thanks
There are in the order that you’re code says they should be, you’re looping through and first showing all the text layouts, then the images and then the videos. If you want to show them in order then all of your if statements need to be inside a single loop.
<?php
if (have_rows('post_content')) {
while (have_rows('post_content')) {
?>
<div>
<?php
the_row();
$layout = get_row_layout();
if ($layout == 'layout_text') {
// code to show text content
} elseif ($layout == 'layout_image') {
// code to show image content
} elseif ($layout == 'layout_video') {
// code to show video content
} else {
// code to do something else
}
?>
</div>
<?php
} // end while
} // end if have_rows
?>
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.