例如,您可以将数据存储在用户元中,作为post id的数组->上次访问时的评论计数,然后简单地计算自该日期以来的评论数
function get_user_comment_count_since_last_visit($user_id ,$post_id){
//only do this for logged in users
if ($user_id <= 0 ){
return 0;
}
/**
* get last comment count for a post id from user meta if set
* and if not set then we define zero
*/
$user_last_visit = get_user_meta( $user_id,\'_last_visit_\',true);
if (!isset($user_last_visit[$post_id]) || $user_last_visit[$post_id] < 0)
$user_last_visit[$post_id] = 0;
/**
* get current comment count of the post
*/
$comments_count = wp_count_comments( $post_id);
/**
* get the amount of added comment since last visit
* and update the user meta for next visit
*/
$user_last_visit[$post_id] = $comments_count->approved - $user_last_visit[$post_id];
update_user_meta( $user_id, \'_last_visit_\', $user_last_visit);
return $user_last_visit[$post_id];
}
要使用它,可以使用当前登录用户的页面id和用户id调用它。