Home › Forums › Add-ons › Flexible Content Field › Post Based On Condition of Field › Reply To: Post Based On Condition of Field
Your loop is definitely rolling around twice, as your HTML currently stands:
<article id="blue" class="post-184 page type-page status-publish hentry">
<div class="container">
<div class="container">
<div class="entry-header">
<h1 class="entry-title">TITLE</h1>
</div>
<div class="col-sm-6">
<h5>SUB-TITLE</h5>
<p>CONTENT</p>
</div>
<div class="col-sm-6">
<h5>SUB-TITLE</h5>
<p>CONTENT</p>
</div>
</div>
</div>
</article>
The only reason for this would be if:
if( have_rows('text_section') ): while ( have_rows('text_section') ) : the_row();
was being called twice, because you had two rows in your field text_section. The first row must be empty, because it’s only calling in the containing <div class=”container”>, and then skipping everything else.
The second one actually has some content in it, so it’s working correctly.
The easiest way to error check this is just to surround the entire thing in a containing if check to make sure that all of the fields you have content in them (you could also set them as required in ACF, which would also solve this)
It’s also worth using the variables that you’ve set at the beginning of the loop, rather than the get_sub_field calls for code cleanliness.
<article id="blue" <?php post_class(); ?>>
<?php if( have_rows('text_section') ): while ( have_rows('text_section') ) : the_row();
// Call in content variables
$title = get_sub_field('title');
$text = get_sub_field('text');
$texts = get_sub_field('text_copy');
$cols = get_sub_field('columns');
// If Title & first column text & columns has been set and is not empty
if( $title && $text && $cols ) :
?>
<div class="container">
<div class="entry-header">
<h1 class="entry-title"><?php echo $title; ?></h1>
</div>
<?php
if($cols == "1") : ?>
<?php echo $text; ?>
<?php elseif( $cols == "2") : ?>
<div class="col-sm-6">
<?php echo $text; ?>
</div>
<div class="col-sm-6">
<?php echo $texts; ?>
</div>
<?php endif; ?>
</div>
<?php
endif; //check title, text, and cols exist
endwhile; //per item in text_section loop
endif; //if text_section has rows
?>
</article>
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.