Php 循环自定义字段图像

Php 循环自定义字段图像,php,foreach,advanced-custom-fields,Php,Foreach,Advanced Custom Fields,因此,我正在使用Wordpress 4.1.1的高级自定义字段插件,并创建了4个自定义字段:“topIMG”、“leftIMG”、“centerIMG”、“rightIMG”。我制作了这些图片,这样我就可以在帖子中添加更多的图片,并将它们放在我页面的特定区域 因此,最初我只想显示一个图像以使其正常工作,这是我显示一个图像的代码: <?php $image = get_field('topimg'); $link = get_field('link', $image['id']); ?>

因此,我正在使用Wordpress 4.1.1的高级自定义字段插件,并创建了4个自定义字段:“topIMG”、“leftIMG”、“centerIMG”、“rightIMG”。我制作了这些图片,这样我就可以在帖子中添加更多的图片,并将它们放在我页面的特定区域

因此,最初我只想显示一个图像以使其正常工作,这是我显示一个图像的代码:

<?php
$image = get_field('topimg');
$link = get_field('link', $image['id']);
?>
<div class="images">
<section id="topIMG">
<img src="<?php echo $image['url']; ?>" />
</section>
</div>

" />
这可以工作并显示图像。现在我尝试创建一个foreach循环,以便也可以显示其余的图像。下面是代码:

<?php
    $array = array('topimg', 'leftimg', 'centerimg', 'rightimg');
    $image = get_field($array);
    $link = get_field('link', $image['id']);
    $output = '<div class="images">';

    foreach($image as $image) {
        $output .= '<section id='.$array.'>';
        $output .= '<img src='.$link.'>';
        $output .= '</section>';
    }
    $output .= '</div>';
    echo $output;
    ?>

当我在网站上查看这些图片时,会出现错误,我不确定如何正确显示这些图片。非常感谢您的帮助

我看到的错误是:

警告:substr()要求参数1为字符串,数组在第268行的/home1/rlebo59/public_html/dev/wp content/plugins/advanced custom fields/core/api.php中给出

警告:第499行/home1/rlebo59/public_html/dev/wp includes/meta.php中的isset中的偏移量类型非法或为空


警告:为第17行/home1/rlebo59/public_html/dev/wp content/themes/RyanPortfolio/single.php中的foreach()提供的参数无效

<?php
$array = array('topimg', 'leftimg', 'centerimg', 'rightimg');
$output = '<div class="images">';
foreach($array as $v){
    $image = get_field($v);
    if($v == 'centerimg'){
        $link = $image['value'];
    }else{
        $link = $image['value']['url'];
    }
    $output .= '<section id='.$v.'>';
    $output .= '<img src='.$link.' />';
    $output .= '</section>';
 }
$output .= '</div>';
echo $output;
?>


前两个错误不在代码的这一部分中……它们似乎在
get_field
函数中,您能发布它吗?好的,我尝试了这个方法,非常感谢您的帮助,但现在我得到了以下两个错误:警告:第1行/home1/rlebo59/public_html/dev/wp content/themes/RyanPortfolio/single.php中的非法字符串偏移量'id'6警告:第17行的/home1/rlebo59/public_html/dev/wp content/themes/RyanPortfolio/single.php中的字符串偏移量“id”非法,这意味着
$image
中有字符串,您无法访问['id']。您需要在
$image=get_字段('topimg')之后对该$image进行var_转储
在您的第一个示例中-那是什么?不是数组?如果第一个示例起作用,这个示例也必须起作用,那么$image也是以同样的方式获得的。因此我的代码看起来像这样?$image=get\u field($v);var\u dump$image正确吗?是的,和/或
$image=get\u field('topimg');var\u dump($image)
从第一个有效的示例开始。为什么要对第一个示例执行此操作?我不想重复所有代码来显示这些图像。我按照您给我的答案执行此操作,但仍然存在这些错误,但“var_dump”会显示该图像的代码