最近有个需求,需要做一下文章“已读”、“未读”标识,下面我们写一下大概的思路及代码。将以下代码添加到主题functions.php
文件中,保存所有用户阅读的帖子数据
add_action('wp_head', function () {
$user_id = get_current_user_id();
# if user is logged in
if (!empty($user_id)) {
$post_id = get_the_ID();
$user_read_post = get_user_meta($user_id, 'read_posts', true);
if (empty($user_read_post)) {
$user_read_post = array(); // if there is no read post
}
if (!empty($post_id)) {
array_push($user_read_post, $post_id);
}
$user_read_post = array_unique($user_read_post);
update_user_meta($user_id, 'read_posts', $user_read_post);
}
});
如何检索用户阅读帖子
$user_id = get_current_user_id();
# if user is logged in
if (!empty($user_id)) {
$user_read_post = get_user_meta($user_id, 'read_posts', true);
}
评论已关闭