我有这个简码,它应该显示最后登录日期给用户。我发现的问题是,所有其他用户都在查看我的最后登录日期,而不是他们的日期。
我的简码有什么问题吗?
// Last Login Shortcode
function user_last_login( $user_login, $user ) {
update_user_meta( $user->ID, 'last_login', date( current_time( 'timestamp' )) );
}
add_action( 'wp_login', 'user_last_login', 10, 2 );
function lastlogin() {
$last_login = get_the_author_meta('last_login');
$the_login_date = date('F j, Y, g:i a', $last_login);
return $the_login_date;
}
add_shortcode('lastlogin','lastlogin');
3条答案
按热度按时间vjhs03f71#
我自己找到了解决办法。我张贴的代码为任何人谁将在同样的问题,因为我。我还扩展了代码,现在你可以同时获得当前登录日期和当前登录之前的最后一次访问日期。
我不太擅长这些东西,我在php,wordpress和代码方面相对较新。我邀请任何人清理代码,并尽可能使其更短和更容易。
1.如果您想显示上次登录日期(而不是当前登录日期),请使用[lastlogin]
2.如果您想在每次新登录时显示当前日期,请使用[currentlogin]。
如何更改日期和时间格式:您可以更改短码1和短码2。编辑
"j M Y - H:i"
,这里有一些有用的信息https://www.php.net/manual/en/datetime.format.php抱歉英语说得不好
mum43rcc2#
谢谢你的澄清。看起来你用错了功能。
get_the_author_meta()
检索当前帖子的作者的请求数据,因此如果您使用该函数,所有用户都将显示帖子作者的日期。因此,在您的情况下,您需要使用
get_user_meta()
你可以在这里找到信息:https://developer.wordpress.org/reference/functions/get_user_meta/
尝试解决方案,如果它不起作用,我们将进一步挖掘。
fwzugrvs3#
我在2022年4月14日试过了。显示2022年3月30日