Skip to content

Instantly share code, notes, and snippets.

@menslow
Last active November 13, 2017 13:10
Show Gist options
  • Save menslow/21845ca34cec4d3851f8 to your computer and use it in GitHub Desktop.
Save menslow/21845ca34cec4d3851f8 to your computer and use it in GitHub Desktop.
Rewrite of the WordPress "get_the_post_thumbnail" for compatibility with jQuery LazyLoad plugin
<php
// Rewrite of "get_the_post_thumbnail" for compatibility with jQuery LazyLoad plugin
function my_get_the_post_lazyload_thumbnail( $post_id = false, $size = 'full' ) {
if ( $post_id ) {
// Get the id of the attachment
$attachment_id = get_post_thumbnail_id( $post_id );
if ( $attachment_id ) {
$src = wp_get_attachment_image_src( $attachment_id, $size );
if ($src) {
$img = get_the_post_thumbnail( $post_id, $size, array(
'class' => 'lazy',
'data-original' => $src[0],
'src' => get_stylesheet_directory_uri() . '/lib/images/transparent.png'
) );
return $img; // returns image tag string or '' (empty string)
}
}
}
return false; // missing either: post_id, attachment_id, or src
}
// Echos out "my_get_the_post_lazyload_thumbnail" function
function my_the_post_lazyload_thumbnail( $post_id, $size ) {
echo ls_get_the_post_lazyload_thumbnail( $post_id, $size );
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment