Skip to content

Instantly share code, notes, and snippets.

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 atelierbram/f9152fd4cb454c3672695ecb521154d2 to your computer and use it in GitHub Desktop.
Save atelierbram/f9152fd4cb454c3672695ecb521154d2 to your computer and use it in GitHub Desktop.
Responsive images in WordPress using srcset with Advanced Custom Fields Pro plugin

Responsive images in WordPress using srcset with Advanced Custom Fields Pro plugin

in functions.php:

add_filter( 'acf_the_content', 'wp_make_content_images_responsive' );

in template.php:

$image = get_field('my_image');
$img_src = wp_get_attachment_image_url( $image['id'], 'full' );											
$img_srcset = wp_get_attachment_image_srcset( $image['id'], 'full' ); ?>

<img src="<?php echo esc_url( $img_src ); ?>"
  width = "<?php echo $image['sizes']['Stripe-width']; ?>"
  height = "<?php echo $image['sizes']['Stripe-height']; ?>"
  srcset="<?php echo esc_attr( $img_srcset ); ?>"
  sizes="(max-width: 100vw) 480px" alt="<?php echo $image['alt']; ?>" />

Source: support.advancedcustomfields.com/forums/topic/wordpress-4-4-responsive-images/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment