2015-04-06 142 views
0

我已经看到如何获取媒体库中的所有图像,反之亦然,从图片库中获取图像,特色缩略图但是没有找到如何基于图像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"; 
} 

回答

0

我发现,如果我分手的图像结果通过属性它的工作原理。

$arr= explode(",",$ids); //prints an array of numbers 

echo "<div id=\"container\">\n"; 
foreach($arr as $id) { 
    $img = wp_get_attachment_image_src($id, medium); 
    echo "<div class=\"$class\"><img src=\"$img[0]\" width=\"$img[1]\" height=\"$img[2]\"></div>\n"; 
} 
    echo "</div>\n"; 
0

您试过wp_get_attachment_image?

<?php wp_get_attachment_image($attachment_id, $size, $icon, $attr); ?> 

wp doc

+0

你能告诉我一个它如何工作的例子吗? – Jstesting

+0

阅读文档<?php echo wp_get_attachment_image(1); ?> – johnnyd23

+0

你能给出循环的完整答案吗? – Jstesting

0

使用wp_get_attachment_image_src来获得图像的URL。

<?php wp_get_attachment_image_src($attachment_id, $size, $icon); ?> 

它返回与对应于(0)URL值的有序阵列,(1)的宽度,(2)的高度,和(3)的图像附接(或者表示任何附件的图标)的规模。

下面是一个例子

$image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'product_image_size'); 
+0

http://codex.wordpress.org/Function_Reference/wp_prepare_attachment_for_js有人可以举一个例子 – Jstesting

+0

看看我编辑的答案的例子.. –

+0

我将如何使用它与每个循环? – Jstesting