我们如何使用短代码显示当前用户的显示名称?

How can we display current user display name with shortcode?

我得到了这个短代码功能来显示当前用户的显示名称,但现在由于某种原因它显示的是用户的名字。有没有其他方法可以获取当前用户的显示名称并使用简码输出?

// [current_user_display_name]
function display_current_user_display_name () {
    $user = wp_get_current_user();
    $display_name = $user->display_name;
    return $user->display_name;
}
add_shortcode('current_user_display_name', 'display_current_user_display_name');

如果您转到用户的个人资料(用户的 > 所有用户 > 搜索您想要的用户),则其中一项设置称为“公开显示名称”。从可用的不同选项中可以看出,用户可以将他们的显示名称设置为他们的名字。如果您希望短代码显示用户名,请使用 $user->user_login。如果您想要名字和姓氏,您可以从 get_user_meta( $user->ID ).

获取该信息

使用名字和姓氏时要小心,因为并非所有用户都会保存名字和姓氏。当没有名字或姓氏时,我通常会回退到使用用户名。