Adding width and height to wp_get_attachment_image_src

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!

Related posts

Leave a Reply

1 comment

  1. 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.