Attention Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/wp get attachment image src


Returns an ordered array with values corresponding to the (0) url, (1) width, (2) height, and (3) scale of an image attachment (or an icon representing any attachment).

It's most often used to get the URL (src) for an image attachment: use the first element in the returned array.

wp_get_attachment_image() uses wp_get_attachment_image_src() to fill the width, height, and src attributes of an img.


 <?php wp_get_attachment_image_src$attachment_id$size$icon ); ?> 


(integer) (required) ID of the desired attachment.
Default: None
(string/array) (optional) Size of the image shown for an image attachment: either a string keyword (thumbnail, medium, large, or full), or a 2-item array representing width and height in pixels, e.g., array(32,32). As of Version 2.5, this parameter does not affect the size of media icons, which are always shown at their original size.
Default: thumbnail
(bool) (optional) Use a media icon to represent the attachment.
Default: false

Return Value

An array containing:
  • [0] => url
  • [1] => width
  • [2] => height
  • [3] => boolean: true if $url is a resized image, false if it is the original or if no image is available.
Returns false if no images is found.


Default Usage

$attachment_id = 8; // attachment ID

$image_attributes = wp_get_attachment_image_src( $attachment_id ); // returns an array
if( $image_attributes ) {
<img src="<?php echo $image_attributes[0]; ?>" width="<?php echo $image_attributes[1]; ?>" height="<?php echo $image_attributes[2]; ?>">
<?php } ?>

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 directory: wp-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's functions.php file:

add_filter( 'icon_dir', 'my_theme_icon_directory' );
add_filter( 'icon_dir_uri', 'my_theme_icon_uri' );

function my_theme_icon_directory( $icon_dir ) {
	return get_stylesheet_directory() . '/images';

function my_theme_icon_uri( $icon_dir ) {
	return get_stylesheet_directory_uri() . '/images'; 

Show the first image of the post

find the full code here get_children().

Change Log

Since: 2.5.0

Source File

wp_get_attachment_image_src() is located in wp-includes/media.php.


Attachment Functions:

get_children(), get attached media(), the_attachment_link(), get_attachment_link(), wp_get_attachment_link(), wp_get_attachment_image(), wp_get_attachment_image_src(), wp_get_attachment_url(), wp_get_attachment_thumb_file(), wp_get_attachment_thumb_url(), is_attachment(), wp_get_attachment_metadata()

See also index of Function Reference and index of Template Tags.