duanjiu4498 2017-06-13 02:08
浏览 22
已采纳

显示前面ACF(Wordpress)的复选框值

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>
  • 写回答

1条回答 默认 最新

  • duanjia6959 2017-06-13 05:56
    关注

    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.

    output

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献