Skip to content

Instantly share code, notes, and snippets.

@djmtype
Last active September 25, 2018 16:31
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 djmtype/ecc5349efb5adfc4ddbb4bdb448e2121 to your computer and use it in GitHub Desktop.
Save djmtype/ecc5349efb5adfc4ddbb4bdb448e2121 to your computer and use it in GitHub Desktop.
ACF Responsive Image
<?php
$image = get_field('my_image');
if( !empty($image) ):
// vars
$size = 'large';
$url = $image['url'];
$title = $image['title'];
$alt = $image['alt'];
$img_srcset = wp_get_attachment_image_srcset( $image['id'], $size );
$img_sizes = wp_get_attachment_image_sizes( $image['id'], $size );
$caption = $image['caption'];
// thumbnail
$thumb = $image['sizes'][ $size ];
$width = $image['sizes'][ $size . '-width' ];
$height = $image['sizes'][ $size . '-height' ];
if( $caption ): ?>
<div class="wp-caption">
<?php endif; ?>
<img src="<?php echo $thumb; ?>" alt="<?php echo $alt; ?>" width="<?php echo $width; ?>" height="<?php echo $height; ?>" srcset="<?php echo esc_attr( $img_srcset ); ?>" sizes="<?php echo esc_attr( $img_sizes ); ?>" />
<?php if( $caption ): ?>
<p class="wp-caption-text"><?php echo $caption; ?></p>
</div>
<?php endif; ?>
<?php endif; ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment