【问题标题】:WP Advanced Custom Fields - True/False field if/else statementWP 高级自定义字段 - 真/假字段 if/else 语句
【发布时间】:2014-10-13 07:50:50
【问题描述】:

我正在使用 ACF 单选按钮字段来切换基于勾选“是”或“否”的代码显示。像这样:

<?php if (get_field('toggle') == 'no'): ?>

    some stuff here

<?php elseif(get_field('toggle') == 'yes'): ?>

    some other stuff here

<?php endif; ?>

但我真正想要的是使用 True/False 字段并让它在未选中时显示一些默认代码,并在选中时显示不同的代码。我只是不确定如何调整上面的代码以反映 True/False 字段的使用。有任何想法吗?提前致谢。

【问题讨论】:

    标签: wordpress if-statement advanced-custom-fields


    【解决方案1】:

    由于只有两个值,您只需检查“已检查”状态,即“是”值或您配置的任何值。

    <?php if ( 'yes' == get_field('toggle') ): ?>
        The box is checked (set to "yes")
    <?php else: ?>
        The box is NOT checked (either false or "no")
    <?php endif; ?>
    

    【讨论】:

    • 啊哈!是的当然。完全有道理。非常感谢!在这种情况下,我们实际上可以只写第一行:
    • 只有在你只关心它是否被设置时才能这样做——例如"false" == true"no" == true
    • 罗杰。再次感谢!
    猜你喜欢
    • 1970-01-01
    • 2015-09-27
    • 2012-01-17
    • 2021-03-22
    • 1970-01-01
    • 2013-10-23
    • 1970-01-01
    • 2021-07-28
    • 2014-03-10
    相关资源
    最近更新 更多