I’m trying to get a User from a User field to show on the front end. It’s in a Repeater. The setup is a Repeater field called ‘staff_notes’ and a User sub_field called ‘staff_member’
What I want is to output the display name with a link to the user’s profile, but even just getting the name would be a start. I’ve looked at the documentation and this is the code I tried:
<?php if( have_rows('staff_notes') ): ?> <?php while ( have_rows('staff_notes') ) : the_row(); ?> <?php $user = get_sub_field('staff_member'); if( $user ): ?> <h3><?php echo $user['display_name']; ?></h3> <?php endif; ?> <?php endwhile; ?> <?php endif; ?>
The User sub_field is using the Array return type with single select, so I saw this code here:
$user = get_field("user_field"); if( $user ): ?> <div class="author-box"> author-avatar <h3><?php echo $user['display_name']; ?></h3> <?php if( $user['user_description'] ): ?> <p><?php echo $user['user_description']; ?></p> <?php endif; ?> </div> <?php endif; ?>
But even changing get_field to get_sub_field (since it’s in a Repeater) didn’t seem to work.
That’s why I added the code for while (have_rows), but I’m still not getting results. I also tried changing the return type (object and ID) and tried changing the User field to a multiselect and using the code for that on the above URL.
How would I get the User’s display name with a link to their profile with the setup I have? As a reminder, the User field is a sub_field in a Repeater.
—- Versions —-
ACF v5.8.7 (PRO)
I did actually manage to get this working, for displaying the display_name at least. The final code I used was:
<span class="staff-note-label">Note Added By:</span> <?php $user = get_sub_field("staff_member"); if( $user ): ?> <span class="staff-note-name"><a href="<?php echo $user['user_url']; ?>"><?php echo $user['display_name']; ?></a></span> <?php endif; ?>
I’m unable to get the url to the User’s profile though. The url is showing as the current post url. I’m sure that user_url is the right meta and I noticed there’s code for the user_url in the multiple selected users example at:
But I tried it with a multi select user field as well and used the second example code and the link is still for the current post and not the user profile url. How can I get the link for the user’s profile in this context?
It does not appear that ACF is returning the url, the value is there but it’s empty
Thank you so much John, that was it. Mystery solved!
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!
Considering recording another "Building a Custom x Block in y minutes with ACF” screencast.— Advanced Custom Fields (@wp_acf) February 26, 2020
🗣 What type of block would be of interest to you?
© 2020 Advanced Custom Fields. Subscribe