【问题标题】:How do I display both the checkboxes and choices from ACF (Wordpress) on front-end?如何在前端显示 ACF (Wordpress) 的复选框和选项?
【发布时间】:2019-09-06 16:03:11
【问题描述】:

我在高级自定义字段中创建了 3 个复选框字段: 奖项提名开放 奖项提名关闭 获奖名单公布

我希望它们都出现在前端,当网络编辑器选择后端的复选框时,前端会出现一个勾号。

我已尝试使用以下帖子中的代码,因为这正是我想要的,但它使我的网站崩溃。 Display checkboxes values from ACF (Wordpress) on front

<div>
 <?php
  $field = get_field_object('table_dates');
  $field_key = "field_5d71ca56bf09b";
   if( $field['choices'] ): ?>
   <ul>
    <?php foreach( $field['choices'] as $value => $label ): ?>
     <li><?php echo $label; ?></li>
    <?php endforeach; ?>
    </ul>
  <?php endif; ?>
</div>

这段代码显示了选项,我如何像后端那样显示刻度?

【问题讨论】:

    标签: php wordpress advanced-custom-fields


    【解决方案1】:

    我稍微修改了您的代码,以便 get_field_object() 包含当前帖子 ID(假设您的代码在循环内)。这样您就可以访问$field['value'] 数组中当前检查的项目。

    还添加了if 语句以将类“活动”添加到所有检查的li 元素。

    <div>
     <?php               
     $field = get_field_object('field_5d71ca56bf09b', get_the_ID());
     if( $field['choices'] ): ?>
      <ul>
       <?php foreach( $field['choices'] as $value => $label ): ?>
        <li <?php echo ( in_array($value, $field['value']) ? 'class="active"' : '' ) ?>><?php echo $label; ?></li>
       <?php endforeach; ?>
      </ul>
     <?php endif; ?>
    </div>
    

    【讨论】:

    • 复选框仍未出现。
    猜你喜欢
    • 1970-01-01
    • 2017-11-14
    • 2013-08-05
    • 1970-01-01
    • 2015-05-31
    • 1970-01-01
    • 1970-01-01
    • 2020-08-30
    • 2013-03-11
    相关资源
    最近更新 更多