Php 将类添加到ACF multi-select
我已经为以下情况挣扎了两个小时,但仍然没有找到解决办法。希望你们能帮我。关于以下内容: 我使用ACF从多选菜单中进行选择。我想显示所有的选择,我想向所选的值添加一个类 下面是我的代码。这将输出所有选择。但我不知道如何检查选择是否被选中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
$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;