wp_get_attachment_image_url()
Gets the URL of the attachment image by the provided image ID. You can also specify the size of the image for which you need to get the URL.
Use wp_get_attachment_url() when you need to get the URL of any attachment. Or when you need to get the URL of the original image file (without specifying the size).
No Hooks.
Returns
String|false. The URL of the image or false if the image does not exist.
Usage
wp_get_attachment_image_url( $attachment_id, $size, $icon );
- $attachment_id(number) (required)
- ID of the image for which you need to get the URL.
- $size(string/array)
The size of the image for which you need to get the URL.
You can specify basic WordPress sizes in this parameter:thumbnail- thumbnail (default 150px x 150px max).medium- Medium size (default 300px x 300px max).large- Large size (default 1024px x 1024px max).full- Full size (the original size of the uploaded image).
You can also specify the size in an array, in the form of two elements (width, height):
array(32, 32). In this case, the most suitable size from the available ones will be selected, and then the image will be compressed/stretched to the specified dimensions.Starting from version 2.5, this parameter does not affect the size of media icons (icons for files), they are always displayed in their original size.
Default: 'thumbnail'- $icon(boolean)
- Set to true to output the URL for media icons for attachments, if it is not an image, but for example a .zip archive.
Default: false
Examples
#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 */
#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() wp get attachment image url code WP 7.0
function wp_get_attachment_image_url( $attachment_id, $size = 'thumbnail', $icon = false ) {
$image = wp_get_attachment_image_src( $attachment_id, $size, $icon );
return $image[0] ?? false;
}