wp_get_attachment_image_sizes() WordPress Function

The wp_get_attachment_image_sizes() function is used to retrieve the image sizes for an attachment. This is useful for displaying the image in different sizes on the front-end.

wp_get_attachment_image_sizes( int $attachment_id, string|int[] $size = 'medium', array $image_meta = null ) #

Retrieves the value for an image attachment’s ‘sizes’ attribute.


Description

Top ↑

See also


Top ↑

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 value: 'medium'

$image_meta

(array)(Optional) The image meta data as returned by 'wp_get_attachment_metadata()'.

Default value: null


Top ↑

Return

(string|false) A valid source size value for use in a 'sizes' attribute or false.


Top ↑

Source

File: wp-includes/media.php

function wp_get_attachment_image_sizes( $attachment_id, $size = 'medium', $image_meta = null ) {
	$image = wp_get_attachment_image_src( $attachment_id, $size );

	if ( ! $image ) {
		return false;
	}

	if ( ! is_array( $image_meta ) ) {
		$image_meta = wp_get_attachment_metadata( $attachment_id );
	}

	$image_src  = $image[0];
	$size_array = array(
		absint( $image[1] ),
		absint( $image[2] ),
	);

	return wp_calculate_image_sizes( $size_array, $image_src, $image_meta, $attachment_id );
}


Top ↑

Changelog

Changelog
VersionDescription
4.4.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More
Show More