skip to Main Content

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


  1. You can use get_user_by( string $field, int|string $value ) get_user_by here is the stackoverflow referance

    Login or Signup to reply.
  2. First you get the user object.

    $user = get_user_by( 'ID', $user_id );
    

    Then display name you get with:

    $display_name = $user->display_name;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search