简体   繁体   English

ACF 复选框隐藏特定值

[英]ACF Checkbox hide particular value

I am using ACF to create some custom fields and then output the results.我正在使用 ACF 创建一些自定义字段,然后输出结果。 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这是管理员的截图

在此处输入图片说明

This is what displays on the front end:这是前端显示的内容:

在此处输入图片说明

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; ?>

With a if statement :使用 if 语句:

<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') : ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM