Home › Forums › General Issues › Getting image url
Hi,
I try to get image url from image field.
I registered the field via php like this:
array (
'key' => 'rectangular_image_field',
'label' => __('Rectangular image(optional)', 'mida'),
'name' => 'rectangular_image',
'type' => 'image',
'instructions' => __('The image will placed in the following sections: special project, main post, single post(bat the bottom of the excerpt)', 'mida'),
),
I understood from your docs that the default return is array, so I put this in the code to check:
$img = get_field('rectangular_image');
echo $img['url'];
The output: 3. (image id)
I tried to change the return_format to url, but I’m still getting image id.
Any help?
The problem is your field key.
field keys must begin with “field_”. The reason that you are getting the id is because ACF does not know what to return and that’s because it does not have a roper field key.
see this documentation http://www.advancedcustomfields.com/resources/register-fields-via-php/
This applies to all fields.
Some fields may return the right thing even with an incorrect field key but that is only because many field types store text and in most cases ACF does not need to figure out what to do with the stored values. In the case of some fields, like image fields, ACF stores the attachment ID and needs the correct field key to find the field settings to see what you want it to return.
you can get the url directly by using field_image.entity.url
The topic ‘Getting image url’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.