
I’m new to flexible fields and, having followed the example code and a few posts in this forum, I’m struggling with if statements on select fields.
This works:
<?php
if( have_rows('text_area') ):
while ( have_rows('text_area') ) : the_row();
if( get_row_layout() == 'text' ) {
the_sub_field('columns');
the_sub_field('first_column_text');
the_sub_field('second_column_text');
}
endwhile;
endif;
?>
But this does not display anything:
<?php
// check if the flexible content field has rows of data
if( have_rows('text_area') ):
while ( have_rows('text_area') ) : the_row();
if( get_row_layout() == 'text' ) {
if(get_sub_field('columns') == "1") {
the_sub_field('first_column_text');
}
} elseif( get_row_layout() == 'text' ) {
if (get_sub_field('columns') == "2") {
the_sub_field('first_column_text');
the_sub_field('second_column_text');
}
}
endwhile;
endif;
?>
And this does not display anything either:
<?php
$first = get_sub_field('first_column_text');
$second = get_sub_field('second_column_text');
$cols = get_sub_field('columns');
// check if the flexible content field has rows of data
if( have_rows('text_area') ):
while ( have_rows('text_area') ) : the_row();
if( get_row_layout() == 'text' ) :
if( $cols == "1") : ?>
<div>
<?php echo $first; ?>
</div>
<?php elseif( $cols == "2") : ?>
<div class="col-sm-6">
<?php echo $first; ?>
<?php echo $second; ?>
</div>
<?php endif;
endif; //get_row_layout
endwhile; //have_rows
endif; //have_rows
?>
Can anyone point out what’s missing here? Thanks!
I fixed it. For anyone else struggling, ths worked for me:
<?php
if( have_rows('text_area') ) :
while ( have_rows('text_area') ) : the_row();
if( get_row_layout() == 'text' ) :
if(get_sub_field('columns') == "1") : ?>
<div>
<?php the_sub_field('first_column_text') ;?>
</div>
<?php elseif(get_sub_field('columns') == "2") : ?>
<div>
<?php the_sub_field('first_column_text'); ?>
<?php the_sub_field('second_column_text'); ?>
</div>
<?php endif;
endif; //get_row_layout
endwhile; //have_rows
endif; //have_rows
?>