高级自定义字段:显示条件数据的多个复选框值 [英] Advanced Custom Fields: multiple checkbox values displaying conditional data

查看:105
本文介绍了高级自定义字段:显示条件数据的多个复选框值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Wordpress中使用高级自定义字段,并且正在使用复选框字段来显示数据,具体取决于所选内容。该字段的返回值设置为值。

I'm using Advanced Custom Fields with Wordpress and I’m using the checkbox field to display data depending on what is selected. The return value of the field is set to 'Value'.

我有两个复选框,并根据是否选择了一个来获取数据。但是同时选中两个复选框时是否可以显示数据?

I have two checkboxes and I’m getting data based on whether one or the other is selected. But is it possible to show data when both checkboxes are selected?

例如:

<?php $options = get_field('options');?>
<?php if( $options && in_array('option-1', $options) ): ?>
  <p>Option 1 selected</p>
<?php elseif ( $options && in_array('option-2', $options) ): ?>
  <p>Option 2 selected</p>
<?php elseif ( $options && in_array('option-1', $options) && in_array('option-2', $options ): ?>
  <p>Option 1 and Option 2 selected</p>
<?php endif;?>

这可能吗?

推荐答案

是的,可以使用 in_array_all 在最后一个 elseif 中,以选中两个复选框。

Yes its possible using in_array_all in your last elseif for checking both checkbox selected.

     <?php $options = get_field('options');
     $options = array($options); ?>
<?php if( $options && in_array(array('option-1','option-2'), $options)): ?>
     <p>Option 1 and Option 2 selected</p>
<?php elseif ( $options && in_array(array('option-2'), $options) ): ?>
  <p>Option 2 selected</p>
<?php elseif ($options && in_array(array('option-1'), $options) ): ?> 
 <p>Option 1 selected</p>
<?php endif;?>

这篇关于高级自定义字段:显示条件数据的多个复选框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆