Created
October 2, 2014 12:02
-
-
Save TorbenL/33d321bd4aae528406a2 to your computer and use it in GitHub Desktop.
crazy lazy the_post_thumbnail() wrapper (quick & dirty)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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