我正在使用代码用户状态How to check if a user (not current user) is logged in?. 在作者的个人资料页面上,效果很好(谢谢大家)。但如果我把它放在文章底部的存档页上,它就不起作用了
functions.php
add_action(\'wp\', \'update_online_users_status\');
function update_online_users_status(){
if(is_user_logged_in()){
if(($logged_in_users = get_transient(\'users_online\')) === false) $logged_in_users = array();
$current_user = wp_get_current_user();
$current_user = $current_user->ID;
$current_time = current_time(\'timestamp\');
if(!isset($logged_in_users[$current_user]) || ($logged_in_users[$current_user] < ($current_time - (15 * 60)))){
$logged_in_users[$current_user] = $current_time;
set_transient(\'users_online\', $logged_in_users, 30 * 60);
}
}
}
author.php
<?php
function is_user_online($user_id) {
// get the online users list
$logged_in_users = get_transient(\'users_online\');
// online, if (s)he is in the list and last activity was less than 15 minutes ago
return isset($logged_in_users[$user_id]) && ($logged_in_users[$user_id] > (current_time(\'timestamp\') - (15 * 60)));
}
$passthis_id = $curauth->ID;
if(is_user_online($passthis_id)){echo \'User is online.\';}
else {echo\'User is not online.\';}
?>
EDIT OP希望“在归档页面的每篇文章底部显示状态”。
SO网友:Matthew Boynes
看来你已经接近这个目标了。
移动此功能:
function is_user_online( $user_id ) {
// get the online users list
$logged_in_users = get_transient( \'users_online\' );
// online, if (s)he is in the list and last activity was less than 15 minutes ago
return isset( $logged_in_users[$user_id] ) && ( $logged_in_users[$user_id] > ( current_time( \'timestamp\' ) - ( 15 * 60 ) ) );
}
您的功能。php文件,并将其从任何其他文件中删除。然后在循环中,下面是您要使用的代码:
<?php
global $post;
if( is_user_online( $post->post_author ) ) : ?>
<p>User is online</p>
<?php else : ?>
<p>User is offline</p>
<?php endif; ?>