我已经看到如何获取媒体库中的所有图像,反之亦然,从图片库中获取图像,特色缩略图但是没有找到如何基于图像ID。在foreach循环中使用媒体ID获取WordPress图像
我正在创建一个自定义画廊简码,并有一个名为ids的属性,就像默认内置的wordpress画廊一样,它将输出基于id的图像。
我看了WordPress文档以及获取图像网址我们需要wp_attachment_src函数。
我有简码:
//他们进入的图像id,而不是发表图片或特色从媒体库缩略图其特定的图像IDS
[一些画廊的id =“8,4的ID ,23,9“]
add_shortcode('some-gallery', 'example_shortcode');
function example_shortcode($atts){
extract(shortcode_atts(array(
'ids' => '8,6,9', // 8 is just a default placement
), $atts));
$arr = explode(",",$ids); //convert list of ids as an array
echo "<div id=\"container\">\n";
foreach($arr as $id) {
$img = wp_get_attachment_image_src($id); //get images using image id not working!!
echo "<div>$img</div>\n"; //result is the word Array
}
echo "</div>\n";
}
你能告诉我一个它如何工作的例子吗? – Jstesting
阅读文档<?php echo wp_get_attachment_image(1); ?> – johnnyd23
你能给出循环的完整答案吗? – Jstesting