一段让wordpress支持jquery lazyload的function代码
下面是function部分
- //WordPress 真正支持 jQuery Lazyload
- function add_image_placeholders( $content ) {
- // Don't lazyload for feeds, previews, robots, mobile
- if( is_feed() || is_preview() || is_robots() || ( function_exists( 'is_mobile' ) && is_mobile() ) )
- return $content;
- // Don't lazy-load if the content has already been run through previously
- if ( false !== strpos( $content, 'data-original' ) )
- return $content;
- // In case you want to change the placeholder image
- $placeholder_image = apply_filters( 'lazyload_images_placeholder_image', get_template_directory_uri() . '/images/loading.gif' );
- // This is a pretty simple regex, but it works
- $content = preg_replace( '#<img([^>]+?)src=[\'"]?([^\'"\s>]+)[\'"]?([^>]*)>#', sprintf( '<img${1}src="%s" data-original="${2}"${3}><noscript><img${1}src="${2}"${3}></noscript>', $placeholder_image ), $content );
- return $content;
- }
- add_filter( 'the_content', 'add_image_placeholders', 99 );
输出的html结构就可以满足lazyload的基本要求了,剩下的交给jq来搞定吧~~(Jq部分这里就不说了,网上够多,具体参考官网http://www.appelsiini.net/projects/lazyload/enabled_fadein.html)
wp的filter太厉害了
是呀 这个貌似是我Google出来的