Support

Account

Home Forums General Issues User Field Type Issues

Solved

User Field Type Issues

  • Hello,

    I have been trying to find more information on the best practice to use the User Field Type. I am trying to use this field to display multiple users attached to a post type or contributors.

    Any guidance would be greatly appreciated! Thank you

    <?php 
    $username = get_field('guests2'); 		
    $userID = $username['ID']; 
    $profile_image = get_field('profile_image', 'user_'. $userID );
    $biography = get_field('biography', 'user_'. $userID );
    ?>
    
    <?php  if ( have_rows('guests2') ) : ?>
    
     	<?php while ( have_rows('guests2') ) : the_row(); ?>
    
    		<div class="view">
                <div class="view-content">
                    <div class="field-content">
                        <a href=""><img class="width175" src="<?php echo $profile_image; ?>"></a>
                    </div>
                </div>
                <div class="guest-name">
                    <div class="field-content"><a href=""><?php echo $username; ?></a></div>
                </div>
                <div class="guest-description">
                    <div class="field-content">
                        <?php echo $biography; ?>
                    </div>
                </div>
            </div>
    
        <?php endwhile; ?>
        
    <?php endif; ?>
  • You’re code is a little confusing. Is guests2 a repeater field or a user field?

  • This is a User field and I am attempting to select multiple users. But maybe is it necessary instead to create a repeater field with only one user select vs multiple user select without repeater?

  • It was confusing because you are using have_rows() which is for a repeater of a flex field and not for a user field.

    What version of ACF are you using?

  • Thanks for the quick replies! I am using Version 5.3.3.2 of ACF Pro.

    I was confused myself whether or not have_rows() was needed when enabling the multi user functionality of the User Field.

    Essentially what my desired functionality is to load multiple usernames on a post and then pull user profile_image and biography taken from their profile fields

  • The user field will return an array of arrays.

    
    $users = get_field('guests2');
    // for testing
    echo '<pre>'; print_r($users); echo '</pre>';
    
    if ($users) {
      foreach ($users as $user) {
        $userID = $user['ID'];
        $profile_image = get_field('profile_image', 'user_'. $userID);
        $biography = get_field('biography', 'user_'. $userID);
        // code for displaying values here
      }
    }
    
  • Thanks so much for the help! I am getting close but one last question. With this snippet I am able to print the assigned users and display the fields. The only issue is for displaying it only shows one of the user biographies despite the print_r showing the two.

    <?php 
    
    $users = get_field('guests2');
    echo '<pre>'; print_r($users); echo '</pre>'; // Prints out assigned user data
    if ($users): ?>
    
      <?php foreach ($users as $user): ?>
    
        <?php setup_postdata($user); 
        $userID = $user['ID'];
        $profile_image = get_field('profile_image', 'user_'. $userID);
        $biography = get_field('biography', 'user_'. $userID); 
       ?>
    	 
         <?php echo $biography; ?> // Only displays first user data rather than all
    	
      <?php endforeach; ?>
    
    <?php wp_reset_postdata(); ?>
    
    <?php endif; ?>
  • don’t do this setup_postdata($user); or this wp_reset_postdata(); these are only needed for posts and don’t do anything for users.

    As far as $biography = get_field('biography', 'user_'. $userID);, I thought this was another custom field. If it’s just the standard WP biography field and your seeing it in the array then use $biography = $user['biography']; Only use get_field() for ACF fields.

  • I’ve used a version of this code for a site I’m developing at the moment. It’s working exactly how I’d like appart from that I’d like to change the size of the avatar image from the standard 96px to 170px. Could someone advise how to edit the code below to achieve this – Thanks!

     <?php $users = get_field('practitioners');
            if ($users): ?>
    
        <?php foreach ($users as $user): ?>
    
        <?php setup_postdata($user); 
            $userid = $user['ID'];
            $userfirst = $user['user_firstname'];
            $userlast = $user['user_lastname'];
            $useravatar = $user['user_avatar'];
            $userdescript = $user['user_description'];
        ?>
    	 
        <div class="authorWrap">
              <?php echo $useravatar; ?>
              <?php echo $userfirst; ?>&nbsp;<?php echo $userlast; ?> -
              <?php echo $userdescript; ?>
        </div>
    	
      <?php endforeach; ?>
    
    <?php wp_reset_postdata(); ?>
    
    <?php endif; ?>

    Thanks!

  • ACF calls get_avatar() https://codex.wordpress.org/Function_Reference/get_avatar and this uses the default values, so the default size should be 96. This can be adjusted by adding a filter for pre_get_avatar https://developer.wordpress.org/reference/hooks/pre_get_avatar/

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

You must be logged in to reply to this topic.