Skip to content

Instantly share code, notes, and snippets.

@verticalgrain
Created December 20, 2016 18:46
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save verticalgrain/384f5c53d1763a20cec45215b7e6999e to your computer and use it in GitHub Desktop.
Save verticalgrain/384f5c53d1763a20cec45215b7e6999e to your computer and use it in GitHub Desktop.
Wordpress srcset snippet for ACF image field
<?php
// Use an ACF image field
// Set the 'return value' option to "array" (this is the default)
// This example uses three image sizes, called medium, medium_large, thumbnail
$imageobject = get_field('image');
if( !empty($imageobject) ):
echo '<img alt="' . $imageobject['title'] . '" src="' . $imageobject['sizes']['medium'] . '" srcset="' . $imageobject['sizes']['medium_large'] .' '. $imageobject['sizes']['medium_large-width'] .'w, '. $imageobject['sizes']['medium'] .' '. $imageobject['sizes']['medium-width'] .'w, '. $imageobject['sizes']['thumbnail'] .' '. $imageobject['sizes']['thumbnail-width'] .'w">';
endif;
?>
@springbreakers
Copy link

This is exactly what I needed. Much appreciated!

@adallaserra
Copy link

Excellent, thanks verticalgrain! I'd make this edit: <img alt="' . $imageobject['alt'] . '" title="' . $imageobject['title'] . '" , but otherwise this is just great and saved me a headache!

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