How to display the status of users (online – offline) in archive.php

It looks like you’re close on this one.

Move this function:

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

to your functions.php file and remove it from any other files. Then in the loop, here’s the code you’ll want to use:

<?php
global $post;
if( is_user_online( $post->post_author ) ) : ?>
    <p>User is online</p>
<?php else : ?>
    <p>User is offline</p>
<?php endif; ?>

Leave a Comment