0

enter image description hereI have added an image through advanced custom field. I want to retrieve image URL. I have tried with following code. It is returning null.

 $url = get_field('aboutus_img','aboutus');

aboutus_img = field name aboutus = field group name

2 Answers 2

1

Refer ACF link : Link

And use

<?php echo get_field('aboutus_img'); ?>

Choose option image url

enter image description here

This example demonstrates how to display the selected image when using the Image URL return type. This return type allows us to efficiently display a basic image but prevents us from loading any extra data about the image.

<?php if( get_field('image') ): ?>
    <img src="<?php the_field('image_field_name'); ?>" />
<?php endif; ?>
6
  • But why is this get_field('aboutus_img','aboutus'); returning null
    – RacingKid
    Jan 1, 2021 at 14:10
  • You are also wrong parameter pass in get field you check my answer some modify
    – HK89
    Jan 1, 2021 at 15:23
  • @HK89 - try to use the actual parameters from the OP so that they can try it directly, without needed to modify the code.
    – Q Studio
    Jan 1, 2021 at 15:25
  • @QStudio , idont know where the problem is , but get_field('aboutus_img') is returning null
    – RacingKid
    Jan 2, 2021 at 4:46
  • you are set location rule in an about page in acf field , and check you are add image in field about page , I suggest to read acf documentation
    – HK89
    Jan 2, 2021 at 5:01
0

ACF provides good documentation about this function, which shows that it has the following signature:

get_field($selector, [$post_id], [$format_value]);
  • $selector (string) (Required) The field name or field key.
  • $post_id (mixed) (Optional) The post ID where the value is saved. Defaults to the current post.
  • $format_value (bool) (Optional) Whether to apply formatting logic. Defaults to true.

You probably need to get the post ID, if you are working outside of a loop - like:

$url = get_field('aboutus_img', get_post_ID() );
2
  • No , I have tried previously with field name. It worked. but this time it Is not
    – RacingKid
    Jan 1, 2021 at 14:04
  • Well, it's the right way to get the field data - it if exists ( has been saved to that post ) - you would need to debug $url to understand if it is empty or not..
    – Q Studio
    Jan 1, 2021 at 15:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.