Search code examples
wordpressadvanced-custom-fieldsacfpro

Display ACF User Field on Frontend


I am trying to display an ACF user field on the front end, but I only want to have the display name and the users email show up. Here is what I have so far, but on the frontend a random string shows up. I debugged the $user variable with print_r() and the array shows all the info correctly. Any ideas would be greatly appreciated.

<?php
// retrieve "pds_project_manager" field from the post id ""
$pds_project_manager = get_field('pds_project_manager', "");
// loop over users
foreach($pds_project_manager as $user){
$user_id = $user['ID'];
$user_email = $user['user_email'];
$user_display_name = $user['display_name'];
echo "$user_display_name ($user_email)";
echo "<br />";
// do something...}

This is the random string thats currently displays on the front end () B (B) W (W) T (T) t (t) B (B) b (b) () 2 (2) () ()


Solution

  • Try this:

    <?php
    $users = get_field("pds_project_manager");
    if( $users ): ?>
    <ul>
        <?php foreach( $users as $user ): ?>
            <li>
                <?php echo esc_attr( $user->display_name ); ?><br/>
                <a href="mailto:<?php echo esc_attr($user->user_email); ?>"><?php echo $user->user_email; ?></a><br/>
                <?php the_field('company_name', 'user_'.$user->ID);?>
            </li>
        <?php endforeach; ?>
    </ul>
    <?php endif; ?>