在页面上显示登录的用户名和姓氏

时间:2021-12-09 作者:Timo Newbie

我想在欢迎网站上显示我的用户的名字。我的函数中有一些代码。让我显示名字的php。如何更改代码以同时显示第一个和最后一个?

这是我的短代码代码:

function custom_shortcode_func() {
    ob_start();
    $current_user = wp_get_current_user();
    echo \'Willkommen \' . $current_user->user_firstname . \'<br />\';
    $output = ob_get_clean();
    return $output;
}
add_shortcode(\'current_user\', \'custom_shortcode_func\');

1 个回复
最合适的回答,由SO网友:WebElaine 整理而成

你总是可以var_dump() 属于$current_user 查看它包含的内容。您将看到有一个类似的属性user_firstname: user_lastname.

您可能需要在名字和姓氏之间留一个空格,因此小规模的编辑应该会添加姓氏:

function custom_shortcode_func() {
    ob_start();
    $current_user = wp_get_current_user();
    echo \'Willkommen \' . $current_user->user_firstname . \' \' . $current_user->user_lastname . \'<br />\';
    $output = ob_get_clean();
    return $output;
}
add_shortcode(\'current_user\', \'custom_shortcode_func\');
正如您所看到的,您已经获得了姓氏和其他数据,因此调整只添加了一个空格字符和姓氏。