dongni8969 2013-07-11 19:13
浏览 27
已采纳

转储数组

I have a fairly basic setup with Wordpress Advanced Custom Fields. I have a need to add extra fields to a custom post and then display them on the post page. I have this code that DOES work, but when I got to a custom field that has multiple checkbox selections, obviously that particular field dumps out the word ‘array’, as it is an array.

How can I make this code below, dump all labels and data for regular fields as well as for fields that have an array in them.

$fields = get_field_objects();
if( $fields )

{
echo '<div class="item-info-custom">';
        echo '<dl class="item-custom">';
        echo '<dt class="title"><h4>Custom Information</h4></dt>';
            foreach( $fields as $field_name => $field )
                {
                    echo '<dt class="custom-label">' .    $field['label'] . ': </dt>';
                    echo '<dd class="custom-data">' . $field['value'] . '</dd>';
                }

        echo '</dl>';
echo '</div>';
}

This is the final code that I got to work:

<?php

$fields = get_field_objects();
if( $fields )

{
echo '<div class="item-info-custom">';
        echo '<dl class="item-custom">';
        echo '<dt class="title"><h4>Custom Information</h4></dt>';
            foreach( $fields as $field_name => $field )
                {
                        echo '<dt class="custom-label">' .         $field['label'] . ': </dt>';
                    echo '<dd class="custom-data">';

if (is_array($field['value'])) {
echo implode(', ', $field['value']);
}
else {
 echo $field['value'];
}

echo '</dd>';
                }

        echo '</dl>';
echo '</div>';
}

?>
  • 写回答

2条回答 默认 最新

  • douyun1852 2013-07-11 19:20
    关注

    You'll need to do some type checking. You can use functions like is_array() and do additional logic.

    For example:

    echo '<dd class="custom-data">';
    
    if (is_array($field['value'])) {
      echo implode(', ', $field['value']);
    }
    else {
      echo $field['value'];
    }
    
    echo '</dd>';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向