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'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

share|improve this question
    
Replace the curly quotes with ". What happens? –  toscho 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. –  toscho Apr 24 '13 at 7:22
    
Where is the ID for the image. –  Simon Cooper Apr 24 '13 at 7:30

1 Answer 1

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
share|improve this answer
    
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

 
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.