I know how to set the size of a post thumbnail image using the WP Admin Media Settings or using this function:
set_post_thumbnail_size( 150, 150 )
But how would one get the image width and height that have been set for an image thumbnail size?
I’d like to be able to do something like this:
get_post_thumbnail_size(); // return array(150, 150)
How would this be done? Is it by using get_option()
, and would the options keys be for thumbnail width and height?
When an image size is added either by WordPress(
add_image_size
), or by a plugin or your own custom code it gets added into the$_wp_additional_image_sizes
global, i can’t find a similar function for pulling data from that global, but you could certainly just look inside the global to determine what width and height a registered image size has.Example:
Of course just be sure to reference those values at a point after you’ve set the size. So if for example you’re setting the thumbnail size during init, you’ll need to make sure to do whatever you need to after that point(else you’ll get back the sizes as they were prior to setting them).
Hope that helps.
There is however another way:
Note:
thumbnail
is one of the sizes set inside$_wp_additional_image_sizes
. So the call toget_option()
will work for every other image size too.The only thing still missing is the thumbnail’s ID:
As an addition to why it’s such a pain to retrieve these numbers, a core enchancement that contains a finished patch. Sadly it’s still not in core.
Just follow the progress of this ticket: WordPress Trac: Enchance
get_intermediate_image_sizes()
to contain not only the names, but the sizes too.Keep in mind that this
global $_wp_additional_image_sizes;
might not work as expected. For example when using is in plugin it will not return the additional sizes that a Themes have registered.What you need is this function: wp_get_attachment_image_src
Codex: http://codex.wordpress.org/Function_Reference/wp_get_attachment_image_src
It will return an array containing attachment’s url, width and height.
In addition to the post above:
Then simply use the next code to display the url, width and height: