Sunday 15 September 2013

wordpress - Get image source by ID with size -


I am working on a WP site with some promotion / ad sliders along with Google Analytics click event. Works great, now I want to serve the right image at the right resolution.

I am using images to serve. Works fine during hard work, so I know that it works when I try to get image source with images uploaded by WP. I know that this is due to my (lackless) php skill.

What image requirements:

  & lt; Span data-image data-alt = "" & gt; & Lt; Span data- src = "filename_default.jpg" & gt; & Lt; / Span & gt; & Lt; Span data-src = "filename_small.jpg" data-media = "(min-width: 400px)" & gt; & Lt; / Span & gt; & Lt; Span data-src = "filename_medium.jpg" data-media = "(min-width: 768px)" & gt; & Lt; / Span & gt; & Lt; Span data-src = "filename_big.jpg" data-media = "(minimum-width: 1200px)" & gt; & Lt; / Span & gt; & Lt ;! - Fallback content for non JS Browser - & gt; & Lt; Noscript & gt; & Lt; Img src = "external / imgs / small.jpg" alt = "" & gt; & Lt; / Noscript & gt; & Lt; / Span & gt;   

I have either a URL or an ID or image stored in the image: $ attachment_id

I thought that:

  Lt; Php $ attachment_id = get_field ('advimg'); $ Big = "ad-pause-a-big"; $ Default = "adv-pos-a-default"; $ Small = "ad-pause-a-small"; ? & Gt;   

get_field ('advimg'); .

  & lt; Span data-image data-alt = "" & gt; & Lt; Span data - src = "& lt ;? php wp_get_attachment_image_src ($ attachment_id, $ default) ;? & gt;" & Gt; & Lt; / Span & gt; & Lt; Span data-src = "& lt ;? php wp_get_attachment_image_src ($ attachment_id, $ small) ;? & gt;" Data-media = "(minimum-width: 400px)" & gt; & Lt; / Span & gt; & Lt; Span data-src = "& lt ;? php wp_get_attachment_image_src ($ attachment_id, $ default) ;? & gt;" Data-media = "(min-width: 768px)" & gt; & Lt; / Span & gt; & Lt; Span data - src = "& lt ;? php wp_get_attachment_image_src ($ attachment_id, $ bigger) ;? & gt;" Data-media = "(minimum-width: 1200px)" & gt; & Lt; / Span & gt; & Lt ;! - Fallback content for non JS Browser - & gt; & Lt; Noscript & gt; & Lt; Img src = "external / imgs / small.jpg" alt = "" & gt; & Lt; / Noscript & gt; & Lt; / Span & gt;   

but it is not working with Iv'e:

wp_get_attachment_image_src wp_get_attachment_image_url wp_get_attachment_image_link

I should be friday, because It is not working and some say that it is not difficult ... I am not seeing this today.

You expected that people could pitch in.

Edit / Last Code / Fix

  & Lt ;? Php $ attachment_id = get_field ('advanced_custom_field_name_here'); $ Small = wp_get_attachment_image_src ($ attachment_id, 'ad-pause-a-small'); $ Default = wp_get_attachment_image_src ($ attachment_id, 'adv-pos-a-default'); $ Big = wp_get_attachment_image_src ($ attachment_id, 'adv-pos-a-large'); ? & Gt; & Lt; Span data-image data-alt = "alt desc here" & gt; & Lt; Span data-src = "& lt ;? Php echo default [0];? & Gt;" & Gt; & Lt; / Span & gt; & Lt; Span data - src = "& lt ;? Php dumb $ small [0] ;? & gt;" Data-media = "(minimum-width: 400px)" & gt; & Lt; / Span & gt; & Lt; Span data - src = "& lt ;? Php echo $ default [0];? & Gt;" Data-media = "(min-width: 768px)" & gt; & Lt; / Span & gt; & Lt; Span data-src = "& lt ;? php echo $ large [0] ;? & gt;" Data-media = "(minimum-width: 1200px)" & gt; & Lt; / Span & gt; & Lt ;! - Fallback material for non-JS browser identical IMG source as the initial, inept source element. - & gt; & Lt; Noscript & gt; & Lt; Img src = "& lt ;? php echo $ default [0] ;? & gt;" Alt = "alt here" & gt; & Lt; / Noscript & gt; & Lt; / Span & gt;  

wp_get_attachment_image_src is given a misleading name (for one The better name would be wp_get_attachment_image_atts) it actually returns the array of an image attachment file with image attributes "url", "width" and "height". For the image source only, use the first element in the returned array:

 $ img_atts = wp_get_attachment_image_src ($ attachment_id, $ default); $ Img_src = $ img_atts [0]; In addition, make sure that your ACF image field is set on the return type attachment ID, so that  $ attachment_id = get_field ('advimg');  gives you the id you expect.   

No comments:

Post a Comment