Support

Account

Home Forums ACF PRO Problems with Select field and get_sub_field('columns')

Helping

Problems with Select field and get_sub_field('columns')

  • 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
    
    ?>
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Problems with Select field and get_sub_field('columns')’ is closed to new replies.