此函数用来获取一个图片的路径函数,此函数返回的是一个包含图片路径,宽度和高度的有序数组。
语法结构
wp_get_attachment_image_src( $attachment_id, $size, $icon );
Array
(
[0] => url //图片地址
[1] => width //图片宽度
[2] => height //图片高度
[3] => boolean //true表示返回了缩放后的图片,false表示返回了原始图片
)
参数
$attachment_id – 数值,必需,想要获取信息的附件ID,默认值:None
size 参数的,只显示原始尺寸。
icon为真可以返回代表视频(mime type:video)的图标,否则只能返回空值。这些代表不同mime type的图片在wp-includes/images/crystal目录下
使用方法
get_post_thumbnail_id($post->ID);获取文章缩略图ID
if (has_post_thumbnail()){
$array_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), array(255,204));
echo $array_image_url[0];
}
官方示例
$attachment_id = 8; // 附件ID
$image_attributes = wp_get_attachment_image_src( $attachment_id ); // 返回一个数组
if( $image_attributes ) {
echo '<img src="'.$image_attributes[0].'" width="'.$image_attributes[1].'" height="'.$image_attributes[2].'" />';
}
欢迎关注我的公众号“xx主题网”,原创技术文章第一时间推送。