检索图像以表示附件。
原型
wp_get_attachment_image_src( int $attachment_id, string|array $size = 'thumbnail', bool $icon = false )
描述
用于图像的文件,缩略图或中间大小的mime图标。
参数
$attachment_id
(int)
(Required)
图片附件ID。
$size
(string|array)
(Optional)
图片尺寸。接受任何有效的图像大小,或以像素为单位的宽度和高度值数组(按此顺序)。
$icon
(bool)
(Optional)
是否应将图像视为图标。
返回值
(false|array)
如果没有可用的图像,则返回一个数组(url,width,height,is_intermediate)或false。
源文件
路径:wp-includes/media.php
<?php
...
function wp_get_attachment_image_src( $attachment_id, $size = 'thumbnail', $icon = false ) {
// get a thumbnail or intermediate image if there is one
$image = image_downsize( $attachment_id, $size );
if ( ! $image ) {
$src = false;
if ( $icon && $src = wp_mime_type_icon( $attachment_id ) ) {
/** This filter is documented in wp-includes/post.php */
$icon_dir = apply_filters( 'icon_dir', ABSPATH . WPINC . '/images/media' );
$src_file = $icon_dir . '/' . wp_basename( $src );
@list( $width, $height ) = getimagesize( $src_file );
}
if ( $src && $width && $height ) {
$image = array( $src, $width, $height );
}
}
/**
* Filters the image src result.
*
* @since 4.3.0
*
* @param array|false $image Either array with src, width & height, icon src, or false.
* @param int $attachment_id Image attachment ID.
* @param string|array $size Size of image. Image size or array of width and height values
* (in that order). Default 'thumbnail'.
* @param bool $icon Whether the image should be treated as an icon. Default false.
*/
return apply_filters( 'wp_get_attachment_image_src', $image, $attachment_id, $size, $icon );
}
...
?>
其他
英文文档:https://developer.wordpress.org/reference/functions/wp_get_attachment_image_src/