Skip to content

Instantly share code, notes, and snippets.

@uptheirons78
Created March 27, 2023 18:24
Show Gist options
  • Save uptheirons78/b39047b464211f8217f5f19139995337 to your computer and use it in GitHub Desktop.
Save uptheirons78/b39047b464211f8217f5f19139995337 to your computer and use it in GitHub Desktop.
WordPress Image Tag with srcset
function storebase_get_image_output($image_id, $size = 'large', $sizes = '(max-width: 768px) 100vw, (max-width: 1024px) 50vw, 33vw') {
$alt = get_post_meta( $image_id, '_wp_attachment_image_alt', true );
$metadata = wp_get_attachment_metadata( $image_id );
$src = wp_get_attachment_image_src( $image_id, $size )[0];
$srcset = wp_get_attachment_image_srcset( $image_id, $size );
$width = $metadata['width'];
$height = $metadata['height'];
$output = '<img src="' . $src . '"';
$output .= ' alt="' . $alt . '"';
$output .= ' srcset="' . $srcset . '"';
$output .= ' sizes="' . $sizes . '"';
$output .= ' width="' . $width . '"';
$output .= ' height="' . $height . '"';
$output .= '/>';
return $output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment