Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get image source by ID with size

I'm working on a WP site with some promotion / advert sliders with a Google Analytics click event. Works great, now I want to serve the right image on the right resolution.

I'm using picturefill for serving the images. Works fine while hardcoded, so I know it works. When I try to get the image sources with images uploaded by WP is doesn't. I know it due my (lack-off) php skills.

What picturfill needs:

<span data-picture data-alt="">
    <span data-src="filename_default.jpg"></span>
    <span data-src="filename_small.jpg" data-media="(min-width: 400px)"></span>
    <span data-src="filename_medium.jpg" data-media="(min-width: 768px)"></span>
    <span data-src="filename_big.jpg" data-media="(min-width: 1200px)"></span>

    <!-- Fallback content for non-JS browsers. -->
    <noscript>
        <img src="external/imgs/small.jpg" alt="">
    </noscript>
</span>

I have either the url or the id or the image stored in: $attachment_id

I thought doing this:

<?php
    $attachment_id = get_field('advimg');
    $large = "adv-pos-a-large";
    $default = "adv-pos-a-default";
    $small = "adv-pos-a-small";

?>

The get_field('advimg'); is from ACF.

<span data-picture data-alt="">
    <span data-src="<?php wp_get_attachment_image_src( $attachment_id, $default ); ?>"></span>
    <span data-src="<?php wp_get_attachment_image_src( $attachment_id, $small ); ?>" data-media="(min-width: 400px)"></span>
    <span data-src="<?php wp_get_attachment_image_src( $attachment_id, $default ); ?>" data-media="(min-width: 768px)"></span>
    <span data-src="<?php wp_get_attachment_image_src( $attachment_id, $large ); ?>" data-media="(min-width: 1200px)"></span>

    <!-- Fallback content for non-JS browsers. -->
    <noscript>
        <img src="external/imgs/small.jpg" alt="">
    </noscript>
</span>

But it ain't working. Iv'e played around with:

wp_get_attachment_image_src wp_get_attachment_image_url wp_get_attachment_image_link

I must be having the fridays, due it ain't working and something says to me that it's not that hard... im just not seeing it today.

Hoped you guys could pitch in.

Thanks in advance,

/Paul

EDIT / FINAL CODE / FIX

<?php 
    $attachment_id = get_field('advanced_custom_field_name_here');
    $small = wp_get_attachment_image_src( $attachment_id, 'adv-pos-a-small' );
    $default = wp_get_attachment_image_src( $attachment_id, 'adv-pos-a-default' );
    $large = wp_get_attachment_image_src( $attachment_id, 'adv-pos-a-large' );
?> 

<span data-picture data-alt="alt desc here">
    <span data-src="<?php echo $default[0]; ?>"></span>
    <span data-src="<?php echo $small[0]; ?>" data-media="(min-width: 400px)"></span>
    <span data-src="<?php echo $default[0]; ?>" data-media="(min-width: 768px)"></span>
    <span data-src="<?php echo $large[0]; ?>" data-media="(min-width: 1200px)"></span>

    <!-- Fallback content for non-JS browsers. Same img src as the initial, unqualified source element. -->
    <noscript>
        <img src="<?php echo $default[0]; ?>" alt="alt desc here">
    </noscript>
</span>
like image 970
Kortschot Avatar asked Jun 21 '13 11:06

Kortschot


People also ask

How do I get an image SRC in WordPress?

Open a page or post where the image is published and locate the image. After that, right-click on the image, and select 'Open image in new tab' option. Once the image opens in the new tab, look at the URL in the address bar. That is your WordPress image URL.

How do I get the URL of a thumbnail?

To get the URL of a post thumbnail you need to add code to the theme template you are customizing. To learn more, refer to our guide on how to add custom code in WordPress. If you simply wanted to display the post thumbnail, then you could paste this code into the template you are working on, inside the WordPress loop.

How do I find the image ID in WordPress?

Log into WordPress admin panel, navigate to Media click on Library. 2. Switch from Grid View to List View, then mouse over the image, you will see an image ID in the bottom.


1 Answers

wp_get_attachment_image_src is confusingly named. (A better name for it would be wp_get_attachment_image_atts). It actually returns an array with the image attributes "url", "width" and "height", of an image attachment file. For just the image src, use the first element in the returned array:

$img_atts = wp_get_attachment_image_src( $attachment_id, $default );
$img_src = $img_atts[0];

Also, make sure your ACF image field return type is set to attachment ID so that $attachment_id = get_field('advimg'); gives you the id that you expect.

like image 194
AidanCurran Avatar answered Oct 20 '22 15:10

AidanCurran