I have been trying several combinations for displaying the display name for a specific user by ID in WordPress, but in the end, I couldn’t make it work. I need help, please. The code I’m trying to use is the following:
function custom_id_display_name($atts, $content = null) {
extract( shortcode_atts(
array('id' => '0',), $atts
)
);
/* return $display_name( $user_id, 96 ); // display the specific user_id's name*/
return get_display_name($atts['id'], 96);
}
add_shortcode('custom_display_name','custom_id_display_name');
The "funny" thing is that the code worked well for custom avatar with a different return,
return get_avatar($atts['id'], 96);
so I have no idea why it’s not working for the display name. Thanks for your help! 🙂
2
Answers
You can use
get_user_by( string $field, int|string $value )
get_user_by here is the stackoverflow referanceFirst you get the user object.
Then display name you get with: