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
?>
The topic ‘Problems with Select field and get_sub_field('columns')’ is closed to new replies.
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.