I’m using ACF to add custom fields to a User. I’m trying to output these fields to the front-end. I was able to pull all fields, but when I was trying to get the img it wouldn’t pull.

Here’s my code:

<?php $user_id = get_query_var( 'author' ) ?>
<?php $clan_member_info = get_userdata($user_id); ?>

<p><?php if(!empty($clan_member_info->psn_id)) echo '<li>' . $clan_member_info->psn_id . '</li>'; ?></p> //Example of me pulling a text field

<?php if(!empty($clan_member_info->cover_photo)) echo '<img src="'.$clan_member_info->cover_photo.'"/>'; ?>

The example of the psn_id pulls perfectly. The img returns this <img src="https://wordpress.stackexchange.com/questions/229180/95">.

In the User section in WP, for my own User I uploaded a photo to the field. In the ACF field group I have the field to get “Image URL”. I was hoping, if you look at the code, to spit the url into the src and output the img.

If I’m missing any data, I could provide a full pastebin url of the php file.

1 Answer
1

When using ACF you should use the methods described in their website documentation because ACF does not store data in the normal WordPress way.

You can use the get_field or the_field functions to retrieve data from your fields.

For example to get data for your field ‘cover_photo’ you could do:

$user_id = get_query_var( 'author' )
$cover_photo = get_field('cover_photo', 'user_'.$user_id);
// Assuming you have this field set to return a url
echo "<img src="https://wordpress.stackexchange.com/questions/229180/$cover_photo">";

For more information see: https://www.advancedcustomfields.com/resources/how-to-get-values-from-a-user/

Leave a Reply

Your email address will not be published. Required fields are marked *