1

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!

2

As written in the documentation, there are no attributes width and height in the fourth parameter of the function. What you might want to do is this:

wp_get_attachment_image(
    $id,
    array( 10, 10 )
);

An alternative and recommended way is to define an image size which will lead WordPress to generate a thumbnail for this size on upload.

add_action( 'after_setup_theme', 'wpse_132171_create_image_size' );
function wpse_132171_create_image_size() {

    add_theme_support( 'post-thumbnails' );
    add_image_size( 'my_size', 10, 10 ); 
}

With that, you can refer to that size by the slug my_size in wp_get_attachment_image():

wp_get_attachment_image( $id, 'my_size' );

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 use attachment-{$val1}x{$val2} (a string built from the array values) as class. This behavior breaks as soon as you use the 4th argument (array attributes) and add a custom class key there. The custom class will override what is delivered by core. This could be considered to be a bug.

|improve this answer|||||
  • Thanks for your help. Where does it say there are no attributes width and height? All I can see that it says is The $attr argument is merged with WordPress's default attributes and passed through the wp_get_attachment_image_attributes filter. but I can't see where I can find out more about this. It doesn't give 'width' or 'height' in the example array, but it also doesn't give 'style' and adding that works fine. I am writing a plugin and I did know about the add_image_size() function but the size of the thumbnails is going to relate to a setting the user sets in the admin panel... – Sarah Feb 2 '14 at 15:09
  • ... by which I mean they enter an input for the maximum width 'eg. 75 pixels' and then I was planning to scale the default thumbnail to that size. I didn't think add_image_size() would work well with this because every time they change the input number the thumbnails would have to be rebuilt. I thought I might as well use the default thumbnail size as it's already there and the image sizes are going to be relatively small so shouldn't affect page load etc. too much. Thanks again for your help, I think I will use the first solution you gave :) – Sarah Feb 2 '14 at 15:12
  • You're right, the documentation is a bit unclear about the possible keys of that attribute arrays. »Merge« is maybe the wrong term there. – David Feb 2 '14 at 16:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.