Last active
May 19, 2020 15:54
-
-
Save Antoinebr/6e38db662cffa2473109499da5f879c9 to your computer and use it in GitHub Desktop.
Hook to use lazysize lib
This file contains 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
// The lazyoad class is added | |
<?php the_post_thumbnail('medium', array('class' => "img-responsive lazyload")); ?> | |
This file contains 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 | |
// | |
// If LazyLoad class is present on the postthubmnail we replace the html | |
// markup to fit the lazysizes html markup plugin requirements | |
// | |
add_filter('post_thumbnail_html','lazyloading_thumb',10,5); | |
function lazyloading_thumb ( $html, $post_id, $post_thumbnail_id, $size, $attr ) { | |
if (strpos($attr['class'], 'lazyload') !== false): | |
$html = str_replace("", 'data-sizes="auto"', $html); | |
$html = str_replace("sizes", "data-sizes", $html); | |
$html = str_replace("srcset", "data-srcset", $html); | |
$html = str_replace("src", "data-src", $html); | |
endif; | |
return $html; | |
} |
This file contains 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 | |
add_action( 'wp_enqueue_scripts', 'register_my_scripts' ); | |
function register_my_scripts() { | |
// get le librairie and save it locally https://github.com/aFarkas/lazysizes/blob/gh-pages/lazysizes.min.js | |
wp_enqueue_script( 'lazysizes', get_template_directory_uri() . '/js/libs/lazysizes.min.js',false, '3.0', true ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment