Skip to content

Instantly share code, notes, and snippets.

@TorbenL
Created October 2, 2014 12:02
Show Gist options
  • Select an option

  • Save TorbenL/33d321bd4aae528406a2 to your computer and use it in GitHub Desktop.

Select an option

Save TorbenL/33d321bd4aae528406a2 to your computer and use it in GitHub Desktop.
crazy lazy the_post_thumbnail() wrapper (quick & dirty)
<?php
function get_the_lazy_post_thumbnail($post_id = 0, $size = '', $attr = array())
{
if(!intval($post_id))
$post_id = get_the_ID();
$thumb = get_the_post_thumbnail($post_id, $size, $attr);
preg_match('~ src="(.*)" ~isU', $thumb, $result);
if(count($result) < 2) return $thumb;
$url = $result[1];
$new_thumb = preg_replace('~ src="(.*)" ~isU', ' src="data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" data-src="'.$url.'" ', $thumb);
$new_thumb = preg_replace('~ height="(.*)" ~isU', ' height="\1" style="max-height:\1px;" ', $new_thumb);
$new_thumb = str_replace(' class="', ' class="crazy_lazy ', $new_thumb);
return $new_thumb.'<noscript>'.$thumb.'</noscript>';
}
function the_lazy_post_thumbnail($size = '', $attr = array())
{
echo get_the_lazy_post_thumbnail(get_the_ID(), $size, $attr);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment