web-dev-qa-db-ja.com

Archive.phpでユーザーの状態(オンライン - オフライン)を表示する方法

私はユーザーステータスのコードを使用しています ユーザー(現在のユーザーではない)がログインしているかどうかを確認する方法? 。作者のページのプロフィール上でそれはうまく動作します(ありがとうみんな)。しかし、私が記事の一番下にあるアーカイブページを表示しても、うまくいきません。

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は「アーカイブページの各投稿の下部にステータスを表示する」ことを望んでいます。

6
Shklyar Sergio

あなたはこれに近づいているようです。

この機能を動かしなさい:

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 ) ) );   
}

functions.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; ?>
1
Matthew Boynes