Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将类添加到ACF multi-select_Php_Wordpress_Select_Multi Select_Advanced Custom Fields - Fatal编程技术网

Php 将类添加到ACF multi-select

Php 将类添加到ACF multi-select,php,wordpress,select,multi-select,advanced-custom-fields,Php,Wordpress,Select,Multi Select,Advanced Custom Fields,我已经为以下情况挣扎了两个小时,但仍然没有找到解决办法。希望你们能帮我。关于以下内容: 我使用ACF从多选菜单中进行选择。我想显示所有的选择,我想向所选的值添加一个类 下面是我的代码。这将输出所有选择。但我不知道如何检查选择是否被选中 $features = get_field_object('features'); $choices = $features['choices']; $values = $features['value']; if ( $features ): ech

我已经为以下情况挣扎了两个小时,但仍然没有找到解决办法。希望你们能帮我。关于以下内容:

我使用ACF从多选菜单中进行选择。我想显示所有的选择,我想向所选的值添加一个类

下面是我的代码。这将输出所有选择。但我不知道如何检查选择是否被选中

$features = get_field_object('features');
$choices = $features['choices'];
$values = $features['value'];

if ( $features ):

    echo '<ul class="checks">';
        foreach ( $choices as $choice) {
            echo '<li>'. $choice .'</li>';
        }
    echo '</ul>';

endif;
$features=get_field_object('features');
$choices=$features['choices'];
$values=$features['value'];
如果($features):
echo'
    ; foreach($choice作为$choice){ 回显“
  • ”.$choice.“
  • ”; } 回声“
”; endif;

非常感谢你的帮助

您可以检查当前$choice的键是否作为$values数组中的值

如果您var_dump$features数组,您将看到$values数组包含值的键,$choices数组包含这两个键

变量转储示例:

$features['value'] => Array
        (
            [0] => color1
        )

$features['choices'] => Array
        (
            [color1] => red
            [color2] => yellow
            [color3] => green
        )
$features = get_field_object('features');
$choices = $features['choices'];
$values = $features['value'];

if ( $features ):

   echo '<ul class="checks">';
   foreach ( $choices as $key => $choice) {
      $class = in_array($key, $values) ? 'class="selected"' : '';
      echo '<li ' . $class . '>'. $choice .'</li>';
   }
   echo '</ul>';

endif;
代码:

$features['value'] => Array
        (
            [0] => color1
        )

$features['choices'] => Array
        (
            [color1] => red
            [color2] => yellow
            [color3] => green
        )
$features = get_field_object('features');
$choices = $features['choices'];
$values = $features['value'];

if ( $features ):

   echo '<ul class="checks">';
   foreach ( $choices as $key => $choice) {
      $class = in_array($key, $values) ? 'class="selected"' : '';
      echo '<li ' . $class . '>'. $choice .'</li>';
   }
   echo '</ul>';

endif;
$features=get_field_object('features');
$choices=$features['choices'];
$values=$features['value'];
如果($features):
echo'
    ; foreach($key=>$choice的选项){ $class=在数组中($key,$values)?'class=“selected”:''; 回显“.$choice.”; } 回声“
”; endif;