我见过如何在媒体库中获取所有图像,反之亦然,从post图片库获取图片,特色缩略图,但没有找到如何基于图像id。
我正在创建一个自定义的图库短代码,并有一个名为id的属性,就像wordpress库中默认的一样,它将输出基于id的图像。
我还查看了WordPress文档,为了获得图像urls,我们需要wp_attachment_src函数。
我有个短码:
//它们输入的ids是图像ids,而不是发布图像或从媒体库获取其特定图像ids的缩略图。
一些画廊ids="8,4,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";
}发布于 2015-04-07 17:40:46
我发现,如果我用属性来分解图像结果,它就会工作。
$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";发布于 2015-04-06 20:38:48
你试过wp_get_attachment_image了?
<?php wp_get_attachment_image( $attachment_id, $size, $icon, $attr ); ?>发布于 2015-04-06 20:44:35
使用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');https://stackoverflow.com/questions/29479226
复制相似问题