Home > Mobile >  Specific user display name by id with shortcode?
Specific user display name by id with shortcode?

Time:05-30

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! :-)

CodePudding user response:

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

CodePudding user response:

First you get the user object.

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

Then display name you get with:

$display_name = $user->display_name;
  • Related