I am using wp_get_attachment_image()
to try and output an img
tag with width
and height
attributes.
Here is my code:
$thumb_src = wp_get_attachment_image( $id, 'thumbnail', false, array('width'=> '10', 'height' => '10' ));
$thumb_output = "<li>".$thumb_src."</li>";
I was expecting this to output:
<li><img width="10" height="10" src="http://..../wp-content/uploads/2011/01/pic-150x150.jpg" class="attachment-thumbnail" alt="pic" width="10" height="10" /></li>
Instead, it is outputting:
<li><img width="150" height="150" src="http://..../wp-content/uploads/2011/01/pic-150x150.jpg" class="attachment-thumbnail" alt="pic" width="10" height="10" /></li>
Thanks very much for any help anyone can give. I have tried it with other attributes, like the style
attribute, and the style tag appears as expected. I guess I could use the style tag to set the dimensions that way but I don’t understand why the width/height method isn’t working!
As written in the documentation, there are no attributes
width
andheight
in the fourth parameter of the function. What you might want to do is this:An alternative and recommended way is to define an image size which will lead WordPress to generate a thumbnail for this size on upload.
With that, you can refer to that size by the slug
my_size
inwp_get_attachment_image()
:For this solution you should rebuild your thumbnails if you have existing images using a plugin like »AJAX Thumbnail Rebuild«
Note: When you use an
array
as 2nd argument, WordPress will useattachment-{$val1}x{$val2}
(a string built from the array values) asclass
. This behavior breaks as soon as you use the 4th argument (array attributes
) and add a customclass
key there. The customclass
will override what is delivered by core. This could be considered to be a bug.