您可以尝试下面的代码片段。我已经开发了短代码,您可以从中参考。
function current_user_data_func( $atts ) {
extract( shortcode_atts( array(
\'metakey\' => \'\',
\'user_field\' => \'\',
), $atts) );
ob_start();
if(is_user_logged_in())
{
$current_user = wp_get_current_user();
$c_user_id = $current_user->ID;
if(!empty($metakey))
{
$value = get_user_meta( $c_user_id, $metakey, true);
echo $value;
}
if(!empty($user_field))
{
echo $current_user->{"$user_field"};
}
}
else
{
echo "User Not Logged In";
}
return ob_get_clean();
}
add_shortcode( \'current_user_data\', \'current_user_data_func\' );
如何使用:
User_fields :
user_login
user_email
user_firstname
user_lastname
display_name
# In template
echo do_shortcode( \'[current_user_data metakey="nickname"]\' );
echo do_shortcode( \'[current_user_data user_field="user_email"]\' );
#in content
[current_user_data metakey="first_name"]
[current_user_data user_field="user_email"]
meta key return only single value if you have stored array value in meta key then you have to remove \'true\' from get_user_meta( $c_user_id, $metakey, true); and get value from that array variable $value. 如果这对你有帮助,请告诉我!