Skip to content

Instantly share code, notes, and snippets.

@jaclyntan
Created February 1, 2017 03:08
Show Gist options
  • Save jaclyntan/2044e64c5105ea3bc38356856f51d934 to your computer and use it in GitHub Desktop.
Save jaclyntan/2044e64c5105ea3bc38356856f51d934 to your computer and use it in GitHub Desktop.
WordPress img srcset
<?php
$id = get_post_thumbnail_id();
$size = 'medium';
$img_src = wp_get_attachment_image_url( $id, $size );
$img_srcset = wp_get_attachment_image_srcset( $id, $size );
$title = get_post($id)->post_title;
$alt = get_post_meta($id, '_wp_attachment_image_alt')[0];
?>
<img src="<?php echo esc_url( $img_src ); ?>"
srcset="<?php echo esc_attr( $img_srcset ); ?>"
sizes="(min-width: 768px) 500px, 100vw"
alt="<?php echo $alt; ?>"
title="<?php echo $title; ?>"
class="feature">
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment