问题描述
尝试显示用ACF创建的复选框值时遇到麻烦。
我有一组12个用ACF制作的复选框,每个复选框是一个月:
jan:1月/ 2月:2月/ 3月:3月/等。
我需要实现的是在前端显示所有这12个值,如果在后端检查了月份,则显示一个特定的CSS类。例如,如果用户选中 Jan, Apr, Jul和 Sep:
< span class = selected> Jan&span>
< span> / span>
< span> Mar< / span>
< span class = selected> Apr< / span>
< span> May< / span>
< span> Jun< / span>
< span class = selected> Jul< / span>
< span> Ago< / span>
< span class = selected> Sep< / span>
< span>十月/ span>
< span> Nov< / span>
< span> Dec< / span>
使用文档中的示例代码(
I'm having some trouble trying to display checkboxes values created with ACF.I have a group of 12 checkboxes made with ACF, where each checkbox is a month:jan : Jan / feb : Feb / mar : Mar / etc.
What i need to achieve is to display all of those 12 values in the frontend, with a particular CSS class if the month has been checked in the backend. For example, if the user checks "Jan", "Apr", "Jul" and "Sep":
<span class="selected">Jan</span>
<span>Feb</span>
<span>Mar</span>
<span class="selected">Apr</span>
<span>May</span>
<span>Jun</span>
<span class="selected">Jul</span>
<span>Ago</span>
<span class="selected">Sep</span>
<span>Oct</span>
<span>Nov</span>
<span>Dec</span>
Using the example code from the docs (https://www.advancedcustomfields.com/resources/checkbox/), I'm only getting the selected months but not all months in the frontend:
<span>Jan</span>
<span>Apr</span>
<span>Jul</span>
<span>Sep</span>
Please check below code and output it helps you.
<?php $allCheckbox = get_field('month_checked_boxes'); //Checked value from backend
$field_key = "field_593f77cdc5068"; //Get value using key
$post_id = get_the_ID();
$field = get_field_object($field_key, $post_id);
foreach($field['choices'] as $lab => $val){
if(in_array($val, $allCheckbox)){
$checked = 'checked = "checked"';
$enable = '';
} else {
$checked = '';
$enable = 'disabled=""';
} ?>
<input type="checkbox" name="month" id="month" value="<?php echo $lab; ?>" <?php echo $enable; ?> <?php echo $checked; ?> /><?php echo $val; ?><br>
Please change field key as par your checkbox field from ACF. Also check output below.
这篇关于在前面显示来自ACF(Wordpress)的复选框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!