1

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) () ()

Webbb
  • 31
  • 7

1 Answers1

0

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; ?>
Cray
  • 5,307
  • 11
  • 70
  • 166
  • Thank you, i have been messing with this and it just returns bullet points. – Webbb Mar 16 '22 at 20:40
  • Also, I have an Acf field that shows a company name for each user, how do you think I should incorporate that? The field is company_name – Webbb Mar 17 '22 at 00:07
  • @Webbb I've changed the code to add the `company_name` field. If the code returns only bullet points, you have to change the return format to object (ACF field settings). I guess at the moment it's an array. – Cray Mar 17 '22 at 11:03
  • Thank you, that worked and thank you for the quick response. I will mess with the company_name some more, but right now it doesn't show the company names from the users. using the_field('company_name', 'user_6') will work to show a single users company, but I am wanting to show the company name for each user that is selected in the pds_project_manager field. – Webbb Mar 17 '22 at 16:59
  • You're right. Try this: `ID);?>`. See the code in my answer. If the answer works, it would be nice if you accept it :) – Cray Mar 17 '22 at 18:54