wp_get_attachment_image_src( int $attachment_id, string|int[] $size = 'thumbnail', bool $icon = false ): array|false
Retrieves an image to represent an attachment.
Parameters
-
$attachment_id
int Required -
Image attachment ID.
-
$size
string|int[] Optional -
Image size. Accepts any registered image size name, or an array of width and height values in pixels (in that order). Default
'thumbnail'
.Default:
'thumbnail'
-
$icon
bool Optional -
Whether the image should fall back to a mime type icon.
Default:
false
Return
array|false Array of image data, or boolean false if no image is available.
- stringImage source URL.
1
intImage width in pixels.2
intImage height in pixels.3
boolWhether the image is a resized image.
Source
File: wp-includes/media.php
.
View all references
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 );
if ( $src ) {
/** 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 ) = wp_getimagesize( $src_file );
}
}
if ( $src && $width && $height ) {
$image = array( $src, $width, $height, false );
}
}
/**
* Filters the attachment image source result.
*
* @since 4.3.0
*
* @param array|false $image {
* Array of image data, or boolean false if no image is available.
*
* @type string $0 Image source URL.
* @type int $1 Image width in pixels.
* @type int $2 Image height in pixels.
* @type bool $3 Whether the image is a resized image.
* }
* @param int $attachment_id Image attachment ID.
* @param string|int[] $size Requested image size. Can be any registered image size name, or
* an array of width and height values in pixels (in that order).
* @param bool $icon Whether the image should be treated as an icon.
*/
return apply_filters( 'wp_get_attachment_image_src', $image, $attachment_id, $size, $icon );
}
Hooks
-
apply_filters( 'icon_dir',
string $path ) -
Filters the icon directory path.
-
apply_filters( 'wp_get_attachment_image_src',
array|false $image ,int $attachment_id ,string|int[] $size ,bool $icon ) -
Filters the attachment image source result.
Changelog
Version | Description |
---|---|
2.5.0 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.
The #return section gives keys, but the function just returns indexes. We should make it clearer what is returned by this function. And some description or a link to explain
is_intermediate
Default Usage
Show the first image associated with the post
This function retrieves the first image associated with a post.
To get original size URL image in the function
wp_get_attachment_image_src
use:Tips: Return values ( [1] width [2] height [3] resize ) seems to be empty when using the Jetpack plugin.
Change Icon Directory
WordPress can use media icons to represent attachment files on your blog and in the Admin interface, if those icons are available.
For images, it returns the thumbnail. For other media types, it looks for image files named by media type (e.g.,
audio.jpg
) in the directorywp-includes/images/crystal/
.This example shows how you can change this directory to a folder called “images” in your theme:
wp-content/themes/yourtheme/images
. Create the folder and put “media type images” in there. To tell WordPress the directory has changed, put this in the current theme’sfunctions.php
file:Retrieve the post thumbnail url sized as 220 if thumbnail exists.
A couple of notes on this page mention getting the first image in a post based on
get_children
. Unfortunately, that won’t work if you’re using the Gutenberg editor. To get the first image from a Gutenberg-based post, you need to parse the blocks. Here’s a function that will return the ID of the first image in a post, ornull
if there is none.Here is my code to create custom gallery from post attachment only (I use it in ‘singl.php‘ for specified category)
the function returns: