Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save rafasashi/f3748e663bf3fc2b68dc88408bb2bf39 to your computer and use it in GitHub Desktop.
Save rafasashi/f3748e663bf3fc2b68dc88408bb2bf39 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 my_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