因此,我正在使用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行的isset中的非法偏移类型或/home1/rlebo59/public_html/dev/wp-includes/meta.php中的空类型
警告:为第17行的/home1/rlebo59/public_html/dev/wp-content/themes/RyanPortfolio/single.php中的foreach()提供的无效参数
发布于 2015-03-10 19:53:03
您必须遍历这个数组才能使用每个图像:
<?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;
?>https://stackoverflow.com/questions/28972772
复制相似问题