URL through custom field returning null

URL through custom field returning nullI 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

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Refer ACF link : Link

And use

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

Choose option image url

URL through custom field returning null

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; ?>

Method 2

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() );


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x