Home Forums Add-ons Repeater Field Displaying Data from 'User' Field Type


Displaying Data from 'User' Field Type

  • 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(); ?>
    			<?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'); ?>
        <?php endwhile; ?>
    <?php endif;
  • The user field returns an array that looks like this:

        [ID] => 6
        [user_firstname] => John
        [user_lastname] => Doe
        [nickname] => johndoe
        [user_nicename] => johndoe
        [display_name] => John Doe
        [user_email] => [email protected]
        [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’)[10] 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 and let WP do all the work.

  • Do you have an example of how one might dynamically populate the User array with a user? I’d like to dynamically add the the post creator to the User array.

Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘Displaying Data from 'User' Field Type’ is closed to new replies.