0

I'm trying to lazy load a page of images in Wordpress - I've tried the plugins but none of them seem to work(I only want to lazy load image on some pages.)

So I'm now trying to do it with a plugin - http://www.appelsiini.net/projects/lazyload

This plugin requires the image width and height in the img tag.

    <img data-original=“img/example.jpg” src=“img/grey.gif” width=“640” height=“480”>

I'm attaching the images from custom fields like

    <img src="<?php echo get_post_meta($post->ID, 'img1', true); ?>">

Is it possible get the images width and height from the get_post_meta

I've looked at wp_get_attachment_image_src but I can't see how to use it get_post_meta

  • Replace the curly quotes with ". What happens? – fuxia Apr 24 '13 at 7:06
  • <img src='<?php echo get_post_meta($post->ID, 'img1', true); ?>'> nothing happens – Simon Cooper Apr 24 '13 at 7:20
  • Try wordpress.stackexchange.com/a/7094/73 to get the attachment ID from a path. But you should really store the ID, not the path in your custom field. – fuxia Apr 24 '13 at 7:22
  • Where is the ID for the image. – Simon Cooper Apr 24 '13 at 7:30
0

Looks like you have the image URL saved as a custom field and not the attachment id so first get the attachment id using the URL using Philips awesome function and then use wp_get_attachment_image_src() to get the attachment attributes :

$attch_id = pn_get_attachment_id_from_url(get_post_meta($post->ID, 'img1', true));
$image_attributes = wp_get_attachment_image_src( $attch_id ); // returns an array

//$image_attributes containing
$image_attributes[0] => URL
$image_attributes[1] => width
$image_attributes[2] => Height
  • Does Philips awesome function go in functions.php – Simon Cooper Apr 24 '13 at 7:45
  • OK I've got it working now but it's giving me the images in the wrong sizes. It's giving me the correct images but in the thumbnails size set in the Media Settings not the size of the image the url relates to in the custom field. 'img1' etc. – Simon Cooper Apr 24 '13 at 7:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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