我正在尝试使用Controller从ACF输出图像。我正在获取图像,但显示两次。我试图找出原因。我的代码如下:
控制器:
public function productsLoop()
{
$products_posts = get_posts([
'post_type' => 'product',
'post_status' => 'publish',
]);
return array_map(function ($post){
return [
'title' => get_the_title($post->ID),
'mainImage' => get_field('product-image', $post->ID),
'content' => apply_filters('the_content', $post->post_content),
'images' => get_field('products-gallery', $post->ID),
];
}, $products_posts);
HTML:
@foreach($products_loop as $products_post)
<div class="row">
<div class="col-sm-3 prod-post-gallery">
@if($products_post['images'])
@foreach($products_post['images'] as $image)
<img src="{!! $image['url'] !!}">
@endforeach
@endif
</div>
<div class="col-sm-9">
@if(!empty($products_post['mainImage']))
@foreach($products_post['mainImage'] as $mainImage)
{!! wp_get_attachment_image($mainImage) !!}
@endforeach
@endif
</div>
</div>
</div>
当我尝试使用$mainImage['url']
而不是w p_get_attachment_image($mainImage)
时,我得到警告:字符串偏移量'url'无效。我用的是鼠尾草锅炉。