We’ve been using the default custom fields in WordPress for years (almost 3,000 posts use them) and now want to switch over to the easier system of using ACF. This however seems to be a problem for us… 🙁
For example: We currently have “thumbnail_large” as a custom field and then add the image’s URL as the value. We call it like this:
<img class="thumbnail_large" src="<?php echo get_post_meta($post->ID, "thumbnail_large", true); ?>" />
Is there any way to edit ACF so that it just outputs URLs? Maybe some other solution could be suggested, because right now it seems like if we want to use ACF, we’re going to need to setup a condition that looks for our old method or the new method such as this:
<?php if (get_post_meta($post->ID, "thumbnail_large", true)) : ?> <img class="thumbnail_large" src="<?php echo get_post_meta($post->ID, "thumbnail_large", true); ?>" /> <?php elseif (get_post_meta($post->ID, "thumbnail_large", false)): ?> <img class="thumbnail" src="<?php echo get_post_meta($post->ID, "thumbnail", true); ?>" /> <?php else: ?> <img class="thumbnail_large" src="<?php the_field('thumbnail_large'); ?>" alt="" /> <?php endif; ?>
It doesn’t seem like the conditional state above is working either and the image’s source is trying to still use the ACF image id instead of the URL.
Your above code should be working, but I don’t think the middle ‘elseif’ statement is needed. I cant see a situation when it would return true.
You can set the return value for an image field to be either ‘ID’, ‘URL’, or ‘ARRAY’.
Does that answer your question?
Edit: Ok @elliot, I think I figured out what our problem is…
We already have a custom field called “thumbnail_large” that we’ve been using for years before ACF and I just setup ACF to use the same “thumbnail_large” custom image field. This way all of our old posts could have their field’s automatically added as ACFs. I’ve seen this happen before with other custom fields and ACF text fields, and it worked.
In this case, however, it doesn’t seem like it’s doing what we want it to do. By adding an image as the ACF “thumbnail_large” field, it also adds the image’s ID as a default custom field. Because our conditional statement is working as it should, it sees that “thumbnail_large” is true, however, because ACF is adding the value as an id (in the backend) it tries to use just the id as a src and fails.
Is there a way to get the ACF to output the image’s value as the actual URL instead of the id in the backend editor?
The topic ‘Possible to use custom image url instead of id?’ 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!
© 2022 Advanced Custom Fields.