Skip to content

Instantly share code, notes, and snippets.

@stefthoen
Last active October 9, 2024 00:33
Show Gist options
  • Save stefthoen/5890515 to your computer and use it in GitHub Desktop.
Save stefthoen/5890515 to your computer and use it in GitHub Desktop.
WordPress function to create picturefill HTML.
function get_responsive_image($id, $src, $alt = "") {
$large = wp_get_attachment_image_src( $id, 'large' );
$medium = wp_get_attachment_image_src( $id, 'medium' );
$small = wp_get_attachment_image_src( $id, 'thumbnail' );
$output = '<div class="img-responsive">';
$output.= ' <div data-picture data-alt="' . $alt . '">';
$output.= ' <div data-src="' . $small[0] . '"></div>';
$output.= ' <div data-src="' . $medium[0] . '" data-media="(min-width: 786px)"></div>';
$output.= ' <div data-src="' . $large[0] . '" data-media="(min-width: 920px)"></div>';
$output.= ' <div data-src="' . $src . '" data-media="(min-width: 1140px)"></div>';
$output.= ' <noscript>';
$output.= ' <img src="' . $small[0] . '" alt="' . $alt . '">';
$output.= ' </noscript>';
$output.= ' </div>';
$output.= '</div>';
return $output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment