$output 显示不正确

$output displays incorrectly

我遇到了一个问题,我 运行 一个 foreach 循环我希望它只显示 array() 中的内容,但是当我输出循环时它显示 "arrayclient" 而不仅仅是客户端。我做错了什么?

<?php 
            $data_array = array("client","task","brand");

            $output = '<div class="clientele">';

            foreach($data_array as $data) {
                $output .= '<section>';
                $output .= '<img src='. get_template_directory_uri()."/img/{$data}.png />";
                $output .= '<h2>'. $field = get_field_object($data);
                $output .= $field['label']. '</h2>';
                $output .= '<p>'. $field['value']. '</p>';
                $output .= '</section>';
            }
            $output .= '</div>';
            echo $output;
            ?>

$field = get_field_object($) 的原因是因为我正在使用插件在 post 上创建自定义字段并希望显示这些字段,因为 "client" ,"task", 和 "brand" 是那些字段标签。

可以试试吗

$field = get_field_object($data);
$output .= '<h2>'. $field;

而不是

$output .= '<h2>'. $field = get_field_object($data);

?

不知道 get_field_object 是做什么的。它似乎不是 PHP 参考中出现的函数。如果您发布它的代码将会很有帮助。

能否也提供完整的输出?

 $output .= '<h2>'. $field = get_field_object($data);
            $output .= $field['label']. '</h2>';

应该是

  $field = get_field_object($data);
  $output .= '<h2>'.$field['label'].'</h2>';

你应该使用:

$output .= '<h2>'. $field = get_field_object($data)[0];

而不是:

$output .= '<h2>'. $field = get_field_object($data);

与其他自定义字段一样,WordPress 允许存在多个具有相同键的字段值,因此您必须指定要输出的字段值。

感谢大家的帮助!我确实尝试了每一个,看起来 KutePHP 提供的信息似乎效果最好。这是我的最终代码。

<?php 
            $data_array = array("client","task","brand");
            $output = '<div class="clientele">';

            foreach($data_array as $data) {
                $output .= '<section>';
                $output .= '<img src='. get_template_directory_uri()."/img/{$data}.png />";
                $field = get_field_object($data);
                $output .= '<h2>'.$field['label'].'</h2>';
                $output .= '<p>'.$field['value'].'</p>';
                $output .= '</section>';
            }
            $output .= '</div>';
            echo $output;
            ?>