Skip to content

Instantly share code, notes, and snippets.

@zrothauser
Last active August 29, 2015 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zrothauser/a8e27f68d4ccdfb9881c to your computer and use it in GitHub Desktop.
Save zrothauser/a8e27f68d4ccdfb9881c to your computer and use it in GitHub Desktop.
Srcset php
<?php
define( 'MEDIUM_BREAKPOINT', '64em' );
define( 'MEDIUM_BREAKPOINT_CONTENT_WIDTH', '674px' );
$post_thumbnail_id = get_post_thumbnail_id();
if ( $post_thumbnail_id ) :
$landscape_feature_retina = wp_get_attachment_image_src( $post_thumbnail_id, 'landscape-feature-retina' );
$landscape_feature_med = wp_get_attachment_image_src( $post_thumbnail_id, 'landscape-feature-med' );
$landscape_feature_sm = wp_get_attachment_image_src( $post_thumbnail_id, 'landscape-feature-sm' );
$alt_text = get_post_meta( $post_thumbnail_id, '_wp_attachment_image_alt', true );
?>
<img src="<?php echo esc_url( $landscape_feature_med[0] ); ?>"
srcset="<?php echo esc_url( $landscape_feature_retina[0] ); ?> <?php echo absint( $landscape_feature_retina[1] ); ?>w,
<?php echo esc_url( $landscape_feature_med[0] ); ?> <?php echo absint( $landscape_feature_med[1] ); ?>w,
<?php echo esc_url( $landscape_feature_sm[0] ); ?> <?php echo absint( $landscape_feature_sm[1] ); ?>w"
sizes="(min-width: <?php echo esc_attr( MEDIUM_BREAKPOINT ); ?>) <?php echo esc_attr( MEDIUM_BREAKPOINT_CONTENT_WIDTH ); ?>,
100vw"
alt="<?php echo esc_attr( $alt_text ); ?>"
class="featured-image" />
<?php endif; ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment