简体   繁体   English

高级自定义字段:显示条件数据的多个复选框值

[英]Advanced Custom Fields: multiple checkbox values displaying conditional data

I'm using Advanced Custom Fields with Wordpress and I'm using the checkbox field to display data depending on what is selected. 我在Wordpress中使用了“高级自定义字段”,并在复选框中使用了根据所选内容显示数据的方法。 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? 但是同时选中两个复选框时是否可以显示数据?

For example: 例如:

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

Is this possible? 这可能吗?

Yes its possible using in_array_all in your last elseif for checking both checkbox selected. 是的,可以在最后一个elseif使用in_array_all来选中两个复选框。

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

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

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