Home › Forums › General Issues › User Name Field – Display Name Only › Reply To: User Name Field – Display Name Only
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; } ?>
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 (  => ID  => user_firstname  => user_lastname  => nickname  => user_nicename  => display_name  => user_email  => user_url  => user_registered  => user_description  => user_avatar )
Just swap the number for the element you’re wanting.
In case this helps anyone else save hours of digging.
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!
Plugin boilerplates can do some of the heavy lifting during initial development. We look at four options to speed up your plugin creation. https://t.co/ZtMsdBxAHw— Advanced Custom Fields (@wp_acf) June 5, 2023
© 2023 Advanced Custom Fields.