Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save nucab/70e9b1a1ba751d19574f8554ebc92262 to your computer and use it in GitHub Desktop.
Save nucab/70e9b1a1ba751d19574f8554ebc92262 to your computer and use it in GitHub Desktop.
/**
* 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($attachment_id = "", $size, $class, $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