Support

Account

Home Forums General Issues Getting image url

Solving

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/

  • Only for image field or to all field?

  • 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.

  • Ok thank you for your answer..

  • you can get the url directly by using field_image.entity.url

Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘Getting image url’ is closed to new replies.