Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to add the width and height attributes to the wp_get_attachment_image_src. Why is this returning width and height:0?

<?php 
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'two' );

if ($image) : ?>
    <img class="attachment-two" src="<?php echo $image[0]; ?>" width="<?php echo $image[1]; ?>" height="<?php echo $image[2]; ?>" />
<?php endif; ?> 

Thank you!

share|improve this question
add comment

1 Answer

What is 'two' referring to? Is that a custom image size?

Place the following var_dump('<pre>',$image,'</pre>'); after your declared $image variable like so,

$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'two' );
var_dump('<pre>',$image,'</pre>');

and provide the results so we can inspect the output.

As per Codex the second argument in this function,

wp_get_attachment_image_src( $attachment_id, $size, $icon );

...refers to the specified size you want returned.

$size (string/array) (optional) Size of the image shown for an image attachment: either a string keyword (thumbnail, medium, large or full) or a 2-item array representing width and height in pixels, e.g. array(32,32). As of Version 2.5, this parameter does not affect the size of media icons, which are always shown at their original size.

    Default: thumbnail

There fore 'two' should refer to the name of the custom image size you added with,

add_image_size( $name, $width, $height, $crop );

http://codex.wordpress.org/Function_Reference/add_image_size

Have you done that?

If yes, did the image exist prior to creating the custom image size or after creating the custom image size?

If the image existed before you created the custom image size then that image in question that you are trying to retrieve does not have the applied image size as a generated version of its original. You would need to regenerate thumbnails to have it apply thats the case.

share|improve this answer
    
Hey Thanks! Yeah, 'two' is a custom size I created, and I think I created it before uploading any image, anyway I run the Regenerate Thumbnails plugin (so useful!) –  Juárez Nov 15 '12 at 14:48
    
I added the var_dump as you said and this is what I get: –  Juárez Nov 15 '12 at 14:49
    
string(5) "<pre>" array(4) { [0]=> string(90) "localhost:8888/dev.test.com/wp-content/uploads/2012/10/…; [1]=> int(0) [2]=> int(0) [3]=> bool(true) } string(6) "</pre>" –  Juárez Nov 15 '12 at 14:49
    
Ok so the array certainly doesn't hold the values you would expect. Can I assume 180x267 is the actual custom width/height you wish to return? Or does your custom 'two' image size have other dimensions? –  userabuser Nov 15 '12 at 14:56
    
You can also specify the size you want returned in the form of an array, so by replacing 'two' with array(100, 100) will return an image size of 100px wide by 100px high. The way it stands, I don't believe that custom image size is applied to the image you are trying to retrieve... –  userabuser Nov 15 '12 at 15:05
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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