如何获得登录用户在wordpress中最后阅读的帖子名称?

时间:2019-06-24 18:42:11

标签: wordpress

我需要获取登录用户在Wordpress中阅读的最新帖子的名称。用作登录用户仪表板上的提示。 “您最近读过的帖子是{post_name}”

请告知我该如何获得最近阅读的帖子ID或名称? 谢谢。

1 个答案:

答案 0 :(得分:0)

在主题的functions.php或插件文件中添加以下代码。

add_action( 'wp', 'wp_user_last_view_post_func' );
function wp_user_last_view_post_func(){
    if (is_user_logged_in() AND is_singular() AND get_post_type()==="post"){
        update_user_meta(get_current_user_id(),'last_view_post_id',get_the_ID());
    }
}

add_action('wp_dashboard_setup', 'last_post_dashboard_widgets');
function last_post_dashboard_widgets() {
    global $wp_meta_boxes;
    wp_add_dashboard_widget('last_read_post_widget', 'Last Read Post', 'last_read_post_widget_dashboard');
}

function last_read_post_widget_dashboard() {
    $last_post_id = get_user_meta(get_current_user_id(),'last_view_post_id',true);
    echo '<p>Your last read post was <a href="'.get_permalink($last_post_id).'" target="_blank">'.get_the_title($last_post_id).'</a></p>';
}

您可以在wordpress仪表板上看到最近阅读的帖子,例如:See Attachment

我希望这对您有帮助。

谢谢