wp_get_attachment_image_url()WP 4.4.0

Get the URL of an image attachment.

1 time — 0.002972 sec (very slow) | 50000 times — 10.29 sec (slow) | PHP 7.1.5, WP 4.8.1

No Hooks.

Return

String|false. Attachment URL or false if no image is available. If $size does not match any registered image size, the original image URL will be returned.

Usage

wp_get_attachment_image_url( $attachment_id, $size, $icon );
$attachment_id(int) (required)
Image attachment ID.
$size(string|int[])
Image size. Accepts any registered image size name, or an array of width and height values in pixels (in that order).
Default: 'thumbnail'
$icon(true|false)
Whether the image should be treated as an icon.
Default: false

Examples

0

#1 Get the Url of the attachment picture

// image
echo wp_get_attachment_image_url( 192 ) .'<br>';
echo wp_get_attachment_image_url( 192, 'full' ) .'<br>';

/* Will be:
http://example.com/wp-content/uploads/2010/12/kolobok3-80x80.jpg
http://example.com/wp-content/uploads/2010/12/kolobok3.jpg
*/

// file
echo wp_get_attachment_image_url( 420 ) .'<br>';
echo wp_get_attachment_image_url( 420, '', 1 ) .'<br>';

/* Will be:
(empty)
http://wp-kama.ru/core/wp-includes/images/media/archive.png
*/
0

#2 Get the URL of the attachment picture and display the IMG

<?php $attach_id = get_post_thumbnail_id() ?>
<img src="<?php echo wp_get_attachment_image_url( $attach_id ); ?>" alt="" />

Changelog

Since 4.4.0 Introduced.

wp_get_attachment_image_url() code WP 6.6

function wp_get_attachment_image_url( $attachment_id, $size = 'thumbnail', $icon = false ) {
	$image = wp_get_attachment_image_src( $attachment_id, $size, $icon );
	return isset( $image[0] ) ? $image[0] : false;
}