I am using wp_get_attachment_image_src
to construct the srcset
attribute of an img
tag. In that attribute I want to output a comma separated list of image urls along with the image widths.
Using the Media Settings page, I have configured the image sizes to be as follows:
- Thumbnail size: 150×150
- Medium size: 640×480
- Large size: 1024×768
- Small size: 278×320
The wp_get_attachment_image_src
function returns the correct urls and the correct widths for the image sizes full
, medium
and small
. small
is a custom size I added using add_image_size
.
If I call wp_get_attachment_image_src( $image_id, 'large' )
, the returned array looks like this:
array (
0 => 'http://localhost/wp-content/uploads/2014/10/image-1024x768.jpg',
1 => 640,
2 => 480,
3 => true,
)
Checking the image which can be found at that url turns up the image with the correct dimensions.
Currently i am using the php builtin function getimagesize to circumvent this problem, but this function call takes significantly longer than simply accessing the index returned by wp_get_attachment_image_src
:
Without getimagesize (mtime before and after execution):
- ‘0.83761000 1415195440’
- ‘0.83831600 1415195440’
With getimagesize:
- ‘0.34808000 1415195635’
- ‘0.35323900 1415195635’
What can I do to make wp_get_attachment_image_src return the correct dimensions?
1 Answer
I found the same issue here
and the solution from there works.
The solution is as simple as pasting this:
$content_width = 2000;// Value higher than your new 'large' width
in functions.php
Explanation copied from there(user Chip Bennett):
I believe your issue is that the value set for the global $content_width variable (which is 640px in Boilerplate and 584px in Twenty Eleven) is less than the width you’re specifying via Settings -> Media.
WordPress is overriding your user setting with the Theme-specific value. This actually makes sense, since a Theme knows its maximum content width, and using a larger image width than what the Theme is designed to accommodate would very likely break the Theme layout.
Check the link above to refer to his full answer.
This is a link to the underscores theme project, where they set this value.