Created
December 20, 2016 18:46
-
-
Save verticalgrain/384f5c53d1763a20cec45215b7e6999e to your computer and use it in GitHub Desktop.
Wordpress srcset snippet for ACF image field
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
?> |
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
This is exactly what I needed. Much appreciated!