user2209033
user2209033

Reputation:

Advanced Custom Fields get sub field image

I have a sub field in ACF, named 'imgcolumn_1'.

This is set to be an image, and allows the user to add an image.

Now, when in PHP, I am trying to retrieve the input the user adds.

For example, I have another sub field in AFC called column_1, which I retrieve using

<?php the_sub_field('column_1');?>

Is there another way to retrieve the image? I am trying the below:

<img src="<?php the_sub_field('imgcolumn_1'); ?>" />

But this doesnt display the image, but instead shows the following:

current image

When I inspect this, I can see the URL is there, but it is surrounded by other numbers?

current inspect element

Any help would be amazing.

Upvotes: 4

Views: 17276

Answers (4)

cwiggo
cwiggo

Reputation: 2599

For me I had to set the ACF contents image sub field return format to 'Image ID'. As seen in the following screenshot.

image id in acf options

Upvotes: 0

Tula Hean
Tula Hean

Reputation: 21

You can use get URL of Image by get_sub_field('imgcolumn_1')['url'];

As function get_sub_field() returns an array.

Upvotes: 1

Doml The-Bread
Doml The-Bread

Reputation: 1771

Do you return a URL an Array or an ID from your field?

looks like an array to me so: Solution 1:

$image = get_sub_field('imgcolumn_1');
<img src="<?php echo $image['url'];?>" />

Solution 2 (if id):

$image = get_sub_field('imgcolumn_1');
<img src="<?php echo get_permalink($image);?>" />

Solution 3 (if url):

$image = get_sub_field('imgcolumn_1');
<img src="<?php echo $image;?>" />

Upvotes: 10

user2209033
user2209033

Reputation:

Figured it out!

I needed to select 'image_url' as the return type!

Upvotes: 0

Related Questions