WordPress获取图像链接、宽和高函数:wp_get_attachment_image_src

WordPress函数wp_get_attachment_image_src用于获取指定ID图像的URL链接、宽度和高度。

wp_get_attachment_image_src( int $attachment_id, string|int[] $size = 'thumbnail', bool $icon = false )

函数参数

$attachment_id

整数

图像附件ID

$size

字符串或数字数组,默认值:thumbnail

指定图像大小,可以是已定义的缩略图名称,或包含宽度和高度的数组。

$icon

布尔值,默认值:false

值为true时,如果附件为非图像文件,将显示对应的图标。

函数返回值

函数返回一个包含图像URL、宽度和高度的数组。

Array ( 
	[0] => https://www.beizigen.com/wp-content/uploads/2020/05/baidusrf-for-linux.webp
	[1] => 768 //宽
	[2] => 430 //高
	[3] => //图像是否为调整的图像
)

函数使用示例

<?php
$image_attributes = wp_get_attachment_image_src(268);
if($image_attributes) : ?>
    <img src="<?php echo $image_attributes[0]; ?>" width="<?php echo $image_attributes[1]; ?>" height="<?php echo $image_attributes[2]; ?>">
<?php endif; ?>

扩展阅读

wp_get_attachment_image_src()函数位于:wp-include/media.php

相关函数:

  • wp_get_attachment_image_url()
  • wp_get_attachment_image_sizes()
  • wp_get_attachment_image_srcset()
  • wp_get_attachment_image_attributes()
  • wp_get_attachment_metadata()
阿里云