Home Forums General Issues Conditionally Show Content Based on 'Choice'


Conditionally Show Content Based on 'Choice'

  • I want to show content based on choice. I have a field with three choices, 1col, 2col, 3col. For now, I just want to echo a field based on the choice made.

    Here’s what I have:

    <?php if( in_array( '1col', get_field('choose_layout') ) ): ?>
         <?php get_field( 'single_column' ); ?>
    <?php endif; ?>

    I referred to this bit of documentation: and am running into a wall.

    Any advice?

  • Hi @digisavvy

    What do you mean by running into a wall?
    I am guessing that the code doesn’t work, but can you be specific as to what part and why?

    Have you attempted to debug the problem by using var_dump to test the values in your code?

  • Sorry about that; I think even I was confused on what I wanted to do…

    The code below gave me what I wanted; I’m always open to tips on optimizing it further.

    <?php if(get_field('choose_layout') == "1col")
    { ?>
       	<p><?php the_field('single_column'); ?></p>
    <?php } elseif (get_field('choose_layout') == "2col")
    { ?>
    	<p><?php the_field('two_columns_left'); ?></p>
    	<p><?php the_field('two_columns_right'); ?></p>
    <?php } elseif (get_field('choose_layout') == "3col")
    { ?>
    	<p><?php the_field('three_columns_left'); ?></p>
    	<p><?php the_field('three_columns_middle'); ?></p>
    	<p><?php the_field('three_columns_right'); ?></p>
    <?php } ?>
Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Conditionally Show Content Based on 'Choice'’ is closed to new replies.