Jay
Jay

Reputation: 360

ACF Checkbox hide particular value

I am using ACF to create some custom fields and then output the results. I have it set so that when you check other in the admin a text box appears where you can enter a value. I don't want 'Other' to display but I can't figure out how to hide it. Can anyone help:

Here is a screenshot of the admin

enter image description here

This is what displays on the front end:

enter image description here

This is my code:

<p>Food Type:</p> 

<?php
$foods = get_field('food_type'); 
$otherFood = get_field('other_food_type'); 
if( $foods ): ?>
<ul>
<?php foreach( $foods as $food ): ?>
    <li><?php echo $food; ?></li>
<?php endforeach; ?>
<?php if( $otherFood ): ?>
<li><?php echo $otherFood ?></li>
<?php endif; ?>
</ul>
<?php endif; ?>

Upvotes: 0

Views: 707

Answers (1)

Alexis Vandepitte
Alexis Vandepitte

Reputation: 2088

With a if statement :

<p>Food Type:</p> 

<?php
$foods = get_field('food_type'); 
$otherFood = get_field('other_food_type'); 
if( $foods ): ?>
<ul>
<?php foreach( $foods as $food ): ?>
    <?php if($food !== 'Other') : ?>
        <li><?php echo $food; ?></li>
    <?php endif; ?>
<?php endforeach; ?>
<?php if( $otherFood ): ?>
<li><?php echo $otherFood ?></li>
<?php endif; ?>
</ul>
<?php endif; ?>

You can also defined values for each checkbox in the field settings and compare with it. So if someday you want to change "Other" for "Other choice" for example, it won't break anything.

-> In your field creation and where you have set the choice, add the value like that :

American
British
[...]
other : Other

and in the condition compare like that:

<?php if($food !== 'other') : ?>

Upvotes: 3

Related Questions