Support

Account

Home Forums Front-end Issues Render field only if value = "YES"

Solving

Render field only if value = "YES"

  • I have a Text field that contains YES or NO. The values come from an external software so I cannot change it. The filed needs to be a Text field.

    I would like to render this filed in front-end only if the value = YES.

    At the moment I am using this code, but the field is rendered also if the value is set to “NO”

    <?php if( get_field('doors') ): ?>
    	<div class="feature">With doors</div>
    <?php endif; ?>

    Many thanks in advance!

  • You are attempting to use ACF to get a field that is not an ACF field. The only reason that it works at all is that it is a simple text field.

    Since this is not an ACF field then you need to use get_post_meta() and you need to test the value to see if it is yes or no

    
    $value = get_post_meta($post->ID, 'doors', true);
    if ($value == 'YES') 
      ?><div class="feature">With doors</div><?php 
    }
    
  • You are attempting to use ACF to get a field that is not an ACF field. The only reason that it works at all is that it is a simple text field.

    Since this is not an ACF field then you need to use get_post_meta() and you need to test the value to see if it is yes or no

    $value = get_post_meta($post->ID, 'doors', true);
    if ($value == 'YES') 
      ?><div class="feature">With doors</div><?php 
    }

    I will test this, thanks :). BTW, what do you men is not an ACF field?

    I am using ACF to create the Text field called Doors …

  • I could be mistaken, you said

    The values come from an external software so I cannot change it.

    so I assumed it was not an acf field.

    If you are getting the value from somewhere else and storing it in an acf field then the solutions is similar, you must test for the value you want.

    
    $value = get_field('doors');
    if ($value == 'YES') 
      ?><div class="feature">With doors</div><?php 
    }
    
  • When I do that I get a syntax error.

    This is what I wrote

    <?php $value = get_field('doors'); ?>
    <?php if ($value == 'YES') ?>
    <div class="feature">With doors</div>
    <?php endif; ?>
  • So I was able to fix the problem. However How can I hide filed that have as value NO?

    Now the one with YES are rendered, but the one with NO generate empty white spaces

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.