Remove Dimension from wp_get_attachment_image

Your arguments for both wp_get_attachment_image_url() and wp_get_attachment_image() are in the wrong order – check the linked documentation for details. Additionally, wp_get_attachment_image_url() returns a URL – not an actual image element.

Removing the width and height attributes from <img> elements is
inadvisable: if the layout of the page is in any way influenced by the
size of the image, the layout will “glitch” as soon as the CSS which
specifies the image’s dimensions, or the image itself loads.

Unfortunately, the wp_get_attachment_image() function is currently (as of WordPress 4.4.1) hard-coded to output the width and height <img> attributes (see ticket #14110), so you’ll need to build the image markup yourself. This can be done by taking cues from wp_get_attachment_image()‘s source:

<?php
  $attachment = get_post( $entry['slide_image_id'] );

  if( $attachment ) {
    $img_size_class="full";
    $img_atts = array(
      'src'   => wp_get_attachment_image_url( $entry['slide_image_id'], $img_size_class, false ),
      'class' => 'attachment-' . $img_size_class . ' size-' . $img_size_class,
      'alt'   => trim(strip_tags( get_post_meta( $entry['slide_image_id'], '_wp_attachment_image_alt', true) ) )
    );

    //If an 'alt' attribute was not specified, try to create one from attachment post data
    if( empty( $img_atts[ 'alt' ] ) )
      $img_atts[ 'alt' ] = trim(strip_tags( $attachment->post_excerpt ));
    if( empty( $img_atts[ 'alt' ] ) )
      $img_atts[ 'alt' ] = trim(strip_tags( $attachment->post_title ));

    $img_atts = apply_filters( 'wp_get_attachment_image_attributes', $img_atts, $attachment, $img_size_class );

    echo( '<img ' );
    foreach( $img_atts as $name => $value ) {
      echo( $name . '="' . $value . '" ';
    }
    echo( '/>' );
  }
?>

Leave a Comment

tech