Wordpress & picturefill implementation -
i'm trying implement responsive image in wordpress theme. there few plug-ins available ones tried messed layout.
what replace existing images (not featured images or thumbnails) responsive images, either srcset or
my wordpress theme creates following image sizes:
add_image_size( "maximal", "1900" ); add_image_size( "desktop", "1405" ); add_image_size( "tablet", "981" ); add_image_size( "smalltablet", "768" ); add_image_size( "mobile", "479" );
i have tried replace img tags preg_replace function have never managed capture images , i'm not sure if it's best way it. of images have classes, don't.
in end, replace simple image this:
<img src="<destination"> id="<id>" class="<class">
by this:
<picture> <source srcset="<img src mobile" media="(max-width: 500px)"> <source srcset="<img src small tablets" media="(min-width: 700px)"> <source srcset="<img src tablets" media="(min-width: 950px)"> <source srcset="<img src desktop" media="(min-width: 1200px)"> <source srcset="<img src big screens" media="(min-width: 1600px)"> </picture>
the srcset equivalent ok.
thanks! laurent
i think filter post_thumbnail_html
trick,, code should this..
add_filter('post_thumbnail_html', 'picturefill_html', 99, 5); function picturefill_html(){ $id = get_post_thumbnail_id(); // attachment id $src_small = wp_get_attachment_image_src($id, 'smalltablet'); // new image size url $src_large = wp_get_attachment_image_src($id, 'maximal'); // new image size url $class = $attr['class']; $html = ' <picture> <source srcset="'.$src_small[0].'" media="(max-width: 500px)"> <source srcset="'.$src_large[0].'" media="(min-width: 1200px)"> </picture> '; return $html; }
Comments
Post a Comment