Skip to content

Instantly share code, notes, and snippets.

@pavlo-bondarchuk
Forked from verticalgrain/srcset.php
Created September 25, 2019 11:34
Show Gist options
  • Save pavlo-bondarchuk/4e58c19d14f9bde5af4cb6fb6e31db15 to your computer and use it in GitHub Desktop.
Save pavlo-bondarchuk/4e58c19d14f9bde5af4cb6fb6e31db15 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;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment