我正在使用wordpress网站,用户可以在其中注册和维护自己的个人资料。 登录用户的主要功能之一是在他们的个人资料上显示他们最近阅读的20或10个帖子。 我尝试了这个,但是效果并不理想,只显示了一个帖子。
add_action('get_header','save_single_posts_to_usermeta');
function save_single_posts_to_usermeta(){
if(is_single() && is_user_logged_in()){
$user_id = get_current_user_id();
$read_posts = get_user_meta($user_id,'_read_posts',true);
$most_recent_read_post = get_user_meta($user_id,'_most_recent_post',true);
if(!is_array($read_posts)){
$read_posts = array();
}
if(!isset($read_posts[get_the_ID()])){
$read_posts[get_the_ID()] = 1;
} else {
$read_posts[get_the_ID()] = $read_posts[get_the_ID()]+1;
}
update_user_meta($user_id,'_most_recent_post',get_the_ID());
update_user_meta($user_id,'_read_posts',$read_posts);
}
}
add_shortcode('most_recent_read_post','show_most_recent_read_post');
function show_most_recent_read_post($args=array()){
if(is_user_logged_in()){
$user_id = get_current_user_id();
$last_read_post = get_user_meta($user_id,'_most_recent_post',true);
if($last_read_post){
return "Last Read Post:".get_the_title((int)$last_read_post);
} else {
return "No posts were read yet!";
}
} else {
return "You must be logged in to view your last read post";
}
}
谢谢!