Home › Forums › General Issues › User Name Field – Display Name Only
I’m using ACF to create a ‘Project’ post type, we want to have a list of team members, which are selected from the WordPress Users. I’m having an issue with the ‘User’ field, I’ve tried using the ‘User’ field as normal with the Multi-Select option and the following code:
<?php
$values = get_field('teammembers');
if($values)
{
echo '<ul>';
foreach($values as $value)
{
echo '<li>' . $value . '</li>';
}
echo '</ul>';
}
// always good to see exactly what you are working with
var_dump($values);
?>
But this displays all info about the user along with the profile image:
Array
array(1) { [0]=> array(1) { [“team_member_name”]=> array(11) { [“ID”]=> string(1) “1” [“user_firstname”]=> string(0) “” [“user_lastname”]=> string(0) “” [“nickname”]=> string(15) “NICKNAME” [“user_nicename”]=> string(5) “admin” [“display_name”]=> string(15) “DISPLAYNAME” [“user_email”]=> string(24) “EMAIL ADDRESS” [“user_url”]=> string(0) “” [“user_registered”]=> string(19) “2013-08-19 08:20:47” [“user_description”]=> string(0) “” [“user_avatar”]=> string(236) “” } } }
I’ve also tried using the ‘User’ field in Select mode using the repeater plugin, using the following to display it:
<?php if(get_field('teammembers')): ?>
<ul>
<?php while(has_sub_field('teammembers')): ?>
<li><?php the_sub_field('team_member_name'); ?></li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
This again displays more information than I need, along with the profile image:
1, , , NICK NAME, admin, DISPLAY NAME, EMAIL ADDRESS, , 2013-08-19 08:20:47, , PROFILE IMAGE
How can I simply get it to show only the display name?
Any help would be greatly appreciated.
Hi @ashley
You can use an array like so:
$values = get_field('teammembers');
if($values)
{
echo '<ul>';
foreach($values as $value)
{
echo '<li>' . $value['display_name'] . '</li>';
}
echo '</ul>';
}
Hi elliot, I am attempting to do the same thing, but the username field is part of a Repeater. So it is an array within an array!
Very new to WordPress (though have been kicking goals the last few days!) – can you give me a possible example I can work from?
Obviously the code below is dodge, but show you what i am trying to do. My repeater is ‘notes_added’, it contains a sub-field ‘user_meta’ which currently spits out an array of all user meta information, like ashley described. It also has a sub-field ‘note_text’ – not an array and requires no filtering. I only want to display the NICKNAME of the user on the page, in a table cell.
<?php
if(get_field('notes_added')): ?>
<h1>Notes</h1>
<table>
<?php while(has_sub_field('notes_added')): ?>
<tr><td><?php the_sub_field('user_meta', 'nickname'); ?></td> <td><?php the_sub_field('note_text'); ?></td></tr>
<?php endwhile; ?>
</table>
<?php endif; ?>
Hope you can help, thanks.
Another brave yet misguided attempt – tried to get closer to your example above – am I getting close?
<?php
if( get_field('notes_added') )
{
while( has_sub_field('user_meta') )
{
$values = get_sub_field('user_meta');
$content = get_sub_field('note_text');
echo '<h1>Notes</h1><table>';
foreach($values as $value)
{
echo '<tr><td>' . $value['nickname'] . '</td> <td>'.$content. '</td></tr>';
}
echo '</table>';
}
}
?>
Hi @spacewindow
Please do not ‘blindly code’. Use a debugging function to print out the variable content. You will have your issue fixed in 5 seconds.
Thanks
E
Point taken. I will go and Google how to do some proper debugging. Currently learning WordPress by myself, with very little guidance – and I am not experienced with PHP at all – any chance you can give me a pointer where I’m going wrong in the meantime?
Sorry, I don’t mean to waste other people’s time through laziness, I assure you this is not the case. I’ve spent several hours trying to figure this out already.
Well it took me a day of searching, but I hit upon the solution. Hope this helps someone to avoid the same trouble.
<?php
$values = get_field('notes_added');
if($values)
echo'<h1>Notes</h1>';
{
foreach ($values as $value){
echo '<table><tr><td>'.$value['user_meta']['nickname'].'</td><td>'.$value['note_text'].'</td></tr></table>';
}
}
?>
I have a similar problem. How to display nicknames of users using repeater field and user field. User field works fine when i use it without repeater. When use repeater field and try to display nicknames i see: “1aaaf2<3GMGgGm2<5PppPp2” not nicknames (admin user2 john).
Here is my code:
`<?php while( has_sub_field(‘secret_content_new’) ): ?>
<?php $premium_user = get_sub_field(‘premium_user_list’);?>
<?php if ( $premium_user ) : ?>
<?php foreach($premium_user as $value) {
echo $value[‘nickname’] ;
};?><?php endif;?>
<?php endwhile; ?>`
Hi @pawelpoz
Please see the above response from @spacewindow. His code shows the correct syntax for nickname
Thanks
E
Perhaps this is a difference with newer versions of PHP – but the syntax that @spacewindow provides no longer works – it throws a somewhat vague PHP error of “Warning: Illegal string offset”.
After a lot of digging, I realized that the User field doesn’t apparently return an actual array, but instead returns an array in a string, thus triggering the warning.
Therefore, the following works for retrieving the associative array element by position for nickname.
<?php
$values = get_field('user_field_name');
if($values)
$array = array_values($values);
echo $array[3];
}
?>
For reference on the keys depending on the User info you’re wanting to retrieve, using print_r(array_keys($values)); reveals the following:
Array ( [0] => ID [1] => user_firstname [2] => user_lastname [3] => nickname [4] => user_nicename [5] => display_name [6] => user_email [7] => user_url [8] => user_registered [9] => user_description [10] => user_avatar )
Just swap the number for the element you’re wanting.
In case this helps anyone else save hours of digging.
J
I have a similar problem like ashley.
For my events (events manager) speakers will be selected from the user database (ACF select user). I only need the person’s name.
I am a PHP beginner and ask for help.
I think I need to store the value from the array in a variable
$ values = Get_Field ('speakers');
$ values = $ value ['display_name'];
is that correct?
The result I want to save as a custom field. This I can enter as Atribut in the format of events manager
Now my beginner questions:
What is the function? Where it stands (funktions.php?)? How can I access it?
For a few tips I would be very grateful !!
many greetings
Ingo
@elliot and if the field to pull the user is a sub field? How would this array? I tried several times, but it returns only the first letter of the field $values = get_sub_field('relationship_user');
This:
$values = get_field('user');
if($values)
{
echo '<ul>';
foreach($values as $value)
{
echo '<li>' . $value['display_name'] . '</li>';
}
echo '</ul>';
}
dont work, return first letter of the field
This code by @demo38 work:
$values = get_field('user');
if($values){
$array = array_values($values);
echo $array[5];
Why?
@websul – if you’re loading a User Name Field inside of a Relationship Field, then you might want something like:
<?php
$posts = get_field('relationship_field_name');
if( $posts ): ?>
<ul>
<?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
<?php setup_postdata($post); ?>
<li>
<?php
$values = get_field('user_field_name');
if($values)
$array = array_values($values);
echo $array[5];
}
?>
</li>
<?php endforeach; ?>
</ul>
<?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endif; ?>
You wouldn’t need the sub_field in this case: http://www.advancedcustomfields.com/resources/relationship/
@demo38 Thanks for reply.
Probably for my bad english you don’t understand
I have a repeater field with name “agenda”, in it, i have a sub field “user”, in which case it is a relationship user
Look: https://uploaddeimagens.com.br/imagens/repeater-jpg
@websul If your Repeater Field name is Agenda, you might be able to use something like this:
<?php
// check if the repeater field has rows of data
if( have_rows('agenda') ):
echo '<ul>';
// loop through the rows of data
while ( have_rows('agenda') ) : the_row();
echo '<li>';
// display a sub field value
$values = get_sub_field('user_field_name');
if($values)
$array = array_values($values);
echo $array[5];
}
echo '</li>';
endwhile;
echo '</ul>';
else :
// no rows found
endif;
?>
@demo38 Man, u save me. Thank you very much! Worked perfectly for me, just not understand why Elliot codes do not work. Ex: $value['display_name']
@websul Glad it helped – I’m not sure about the other syntax, I did a lot of troubleshooting a while ago trying to figure things out for the User Field and that’s just what I had pieced together.
The topic ‘User Name Field – Display Name Only’ is closed to new replies.
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!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.