CharlyAnderson
CharlyAnderson

Reputation: 737

Output URL for thumbnail image size

I am using the following code in ACF to display an image, using a particular thumbnail size using image ID.

<?php 
    $image = get_field('services-hero-bg'); $size = 'hero';
    if( $image ) {
        echo wp_get_attachment_image( $image, $size );
    }
 ?>

However, I wish to be able to keep the custom size, but output the URL only.

I have looked through the documentation and found a way to output the URL but this doesn't allow you to select a custom image size.

Update

The results of the print_r:

Test Array ( 
    [ID] => 158 
    [id] => 158 
    [title] => Marquees-4 
    [filename] => Marquees-4.jpg 
    [url] => http://localhost:8888/wp-content/uploads/2017/08/Marquees-4.jpg 
    [alt] => 
    [author] => 1 
    [description] => 
    [caption] => 
    [name] => marquees-4 
    [date] => 2017-08-28 16:17:52 
    [modified] => 2017-08-28 16:17:56 
    [mime_type] => image/jpeg 
    [type] => image 
    [icon] => http://localhost:8888/wp-includes/images/media/default.png 
    [width] => 1024 
    [height] => 683 
    [sizes] => Array ( 
        [thumbnail] => http://localhost:8888/wp-content/uploads/2017/08/Marquees-4-150x150.jpg 
        [thumbnail-width] => 150 [thumbnail-height] => 150 
        [medium] => http://localhost:8888/wp-content/uploads/2017/08/Marquees-4-250x167.jpg 
        [medium-width] => 250 
        [medium-height] => 167 
        [medium_large] => http://localhost:8888/wp-content/uploads/2017/08/Marquees-4-768x512.jpg 
        [medium_large-width] => 768 
        [medium_large-height] => 512 
        [large] => http://localhost:8888/wp-content/uploads/2017/08/Marquees-4-700x467.jpg 
        [large-width] => 700 
        [large-height] => 467 
        [small] => http://localhost:8888/wp-content/uploads/2017/08/Marquees-4-120x80.jpg 
        [small-width] => 120 [small-height] => 80 
        [logo] => http://localhost:8888/wp-content/uploads/2017/08/Marquees-4-400x267.jpg 
        [logo-width] => 400 [logo-height] => 267 
        [hero] => http://localhost:8888/wp-content/uploads/2017/08/Marquees-4-600x683.jpg 
        [hero-width] => 600 
        [hero-height] => 683 
    ) 
)

Upvotes: 0

Views: 273

Answers (1)

Chris
Chris

Reputation: 4728

If the field type is image then get_field should return an array. Within the array there is a parameter called url.

<?php 
    $image = get_field('services-hero-bg'); $size = 'hero';
    if( $image ) {
        echo $image['url'];
    }
?>

e.g.

<?php 
    $image = get_field('services-hero-bg'); $size = 'hero';
    if( $image ) {
        echo '<img src="'.$image['url'].'">';
    }
?>

edit

After you have added the output from print_r($image) I can see that you just need to use this to access the URL of the hero image:

echo $image['sizes']['hero'];

Upvotes: 1

Related Questions