Skip to content

Instantly share code, notes, and snippets.

@matesnippets
Created March 9, 2015 15:13
Show Gist options
  • Select an option

  • Save matesnippets/1d1e707bfcd38e608b13 to your computer and use it in GitHub Desktop.

Select an option

Save matesnippets/1d1e707bfcd38e608b13 to your computer and use it in GitHub Desktop.
WordPress, PHP, Get lazyload friendly attachement image based on attachment ID
/**
* Gets an attachement image as lazy loaded img based on ID
* @param string $size Thumbnail, medium, large, original, or {custom size}
* @param string $class CSS class name for the img tag
* @param integer $attachment_id ID of the attachment
* @param string $attachment_id The custom src atrribute to use, default to data-echo
* @return string Lazyload friendly HTML img tag
*/
function get_attachemt_lazy($size, $class, $attachment_id = "", $src_attr = "data-echo")
{
$src = wp_get_attachment_url($attachment_id);
$attr = array(
'class' => "attachment-$size $class",
'src' => 'data:image/gif;base64,R0lGODlhAQABAIAAAMLCwgAAACH5BAAAAAAALAAAAAABAAEAAAICRAEAOw==',
$src_attr => $src,
);
$output = wp_get_attachment_image($attachment_id, $size, false, $attr);
return $output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment