尝试获取附件网址后未定义的网址
问题描述:
In the backend of WordPress, I have an option where the user can add multiple images. These images will then be explode
'd an shown within the li
, that's the aim anyway.
But I'm receiving undefined index url on $result .= $attachmentImage ? $attachmentImage['url'] : '';
.
Markup:
$image = shortcode_atts(
array(
'image' => 'image',
),
$atts
);
$image_ids = explode(',',$image['image']);
$result = "<div class='imageCarousel'>";
$result .= "<div class='imageCarousel__container justify-content-center'>";
$result .= "<ul>";
foreach( $image_ids as $image_id ){
$result .='<li>';
$result .= 'test'. $image_id;
$attachmentImage = wp_get_attachment_image_src( $image_id, 'full' );
$result .= $attachmentImage ? $attachmentImage['url'] : '';
$result .='</li>';
}
$result .= "</ul>";
$result .= "</div>";
$result .= "</div>";
return $result;
A var_dump($attachmentImage)
returns nothing?
答
Based on the code, while calling the short code need to add the valid image id by comma separated.
like.
//image1 is name of shortcode, and 10,11,12 is valid id of image
echo do_shortcode('[image1 image=10,11,12]');
you are getting Receiving 'undefined index url' because wp_get_attachment_image_src return an array with the following elements.
[0] => url
[1] => width
[2] => height
[3] = >
So in code you have need to get the image url by $attachmentImage['0'].
//$attachmentImage['0'] - will display the url of image
$result .= $attachmentImage ? $attachmentImage['0'] : '';