获取小规模图片网址


get a small-scale image url

我想获取我上传的图像的小规模版本的网址。我保存图像,为其指定标题,然后保存小分辨率图像(150X150 或 148X150)。我知道如何通过标题获取图像网址,但我总是得到全分辨率图像网址。

您有什么建议如何获取 150X150/148X150 图像网址吗?

您希望函数wp_get_attachment_image_src()

<?php 
$attachment_id = 8; // attachment ID
$image_attributes = wp_get_attachment_image_src( $attachment_id, array( 150, 150 ) ); // returns an array
// Alternatively, you can pass a second parameter of 'thumbnail', 'small', 
// 'medium' or 'large' instead of an array
?> 
<img src="<?php echo $image_attributes[0]; ?>" width="<?php echo $image_attributes[1]; ?>" height="<?php echo $image_attributes[2]; ?>">

如果您上传图像,您应该有 4 个文件:

filename-150x150.jpg
filename-resized_widthx300.jpg
filename-resized_widthx1024.jpg
filename.jpg

filename-150x150.jpg
filename-300xresized_height.jpg
filename-1024xresized_height.jpg
filename.jpg

当您将上传的图像插入帖子/页面时,您可以选择要使用的图像大小,例如我可以选择:

Thumbnail (150 × 150)
Medium (195 × 300)
Large (668 × 1024)
Full Size (2618 × 4013)

您需要使用缩略图来获取 150x150 像素图像的网址