Hello, I’m trying to use the ‘User’ field type in a repeater but am having trouble finding documentation on how to display specific information from the User. I just need the User’s display name; however, the_sub_field returns an array of data. I figured get_userdata would work but I don’t seem to be able to get the user’s ID first. I quickly tried using a relationship field loop but that didn’t seem to work either. Also note I am always going to limit each entry to 1.
if ( have_rows('wod_stats') ) : ?> <ul class="wod-stats"> <?php while ( have_rows('wod_stats') ) : the_row(); ?> <li> <?php $wod_stat_member = get_sub_field('wod_stat_member'); ?> <strong>Member:</strong> <?php echo $wod_stat_member->ID; ?><br /> <strong>Stat:</strong> <?php the_sub_field('wod_stat'); ?> </li> <?php endwhile; ?> </ul> <?php endif;
The user field returns an array that looks like this:
Array ( [ID] => 6 [user_firstname] => John [user_lastname] => Doe [nickname] => johndoe [user_nicename] => johndoe [display_name] => John Doe [user_email] => email@example.com [user_url] => [user_registered] => 2016-07-03 12:23:56 [user_description] => [user_avatar] => )
This was generated by adding the following to a template where I want to use the information
echo '<pre>'; print_r(get_field('user_field')); echo '</pre>';
Hope that helps
I’m having trouble with a similar issue. What I want is for the editor on the WP backend to be able to pick a user, and have only their avatar show up on a certain part of the page. The user picker is inside of a repeater field. So like every other field I use the code <?php the_sub_field(‘expert_1’); ?> to display it in my page, with ‘expert_1’ being the field variable name of the user. However, of course, this gives me an array of data, not just the avatar, and the life of me I cannot figure out how to use the array. When I do gettype() on the_sub_field(‘expert_1’), I get NULL, when I feel like I should be getting array. I tried the_sub_field(‘expert_1’) and that didn’t work. The little documentation out there on the user field hasn’t been helpful. Thanks for the help!
I think that
user_avatar in the above array only holds a value if the users upload on, but I’m not sure. I don’t have a lot of experience with this type of field. Usually when I want to display a user avatar I use this WP function https://codex.wordpress.org/Function_Reference/get_avatar and let WP do all the work.
You must be logged in to reply to this topic.
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.