0

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>';
}

?>
4

2 回答 2

1

Depending on the composition of the arrays in $field['value'] you can do one of the following:

If it's a simple list of values you can just tape them together with implode.

echo '<dd class="custom-data">' . (is_array($field['value'])?implode(", ", $field['value']:$field['value']) . '</dd>';

If the array contains data represented like the main array (with label and value keys) you can create a function to render the array and call it recursively when you encounter a array value.

<?php

function showFields($data){
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>';
                    if (is_array($field['value'])){
                        showFields($field['value']);
                    }
                    echo '<dd class="custom-data">' . $field['value'] . '</dd>';
                }

        echo '</dl>';
echo '</div>';
}   
$fields = get_field_objects();
if( $fields ) showFields($fields);
于 2013-07-11T19:21:05.980 回答
0

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>';
于 2013-07-11T19:20:42.740 回答