Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I need to modify the_post_thumbnail function to run "Lazyload" on it, i think there is two solutions :

1- modify the args to be something like this

the_post_thumbnail('product', array('data-original'=>$src, 'src'=>'grey.gif'));

(((NOT WORKING!)))

2- get only the image url from the function ... i've tried alot of snippets and nothing worked for me , like this one !

$thumbnail_src = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), "size" );

any ideas??

thanks

share|improve this question

1 Answer 1

up vote 1 down vote accepted

If you want to apply lazyload to each attachment image, you can just add your hoot to wp_get_attachment_image_attributes filter:

add_filter( 'wp_get_attachment_image_attributes', 'wpse8170_add_lazyload_to_attachment_image', 10, 2 );
function wpse8170_add_lazyload_to_attachment_image( $attr, $attachment ) {
    $attr['data-original'] = $attr['src'];
    $attr['src'] = 'grey.gif';
    return $attr;
}

Or if you can use second approach:

$thumbnail_src = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), "size" );
// add more attributes if you need
printf( '<img src="grey.gif" data-original="%s"/>', esc_url( $thumbnail_src[0] ) ); 
share|improve this answer
    
Thanks alot,the add filter works like charm –  johnmido Jun 12 '12 at 15:41
    
the second approach just like the second solution posted in my post , it just echos "Array" –  johnmido Jun 12 '12 at 15:43
    
oups... already fixed :) –  Eugene Manuilov Jun 12 '12 at 16:04
    
You are the man :) –  johnmido Jun 12 '12 at 16:17
    
If you please, how to make add_filter apply only on specific registered '$size' s of attachments ? –  johnmido Jun 16 '12 at 12:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.