我正在尝试将当前用户的ID(如果用户已登录)保存在一个数组中,在用户阅读的每一篇文章中都使用一个名为“post\\u is\\u read”的post\\u元键,以便我可以执行某些操作,例如将文章标记为“未读”。
我将此添加到single page 模板:
$readers = get_post_meta(get_the_ID(), \'post_is_read\');
$current_user = wp_get_current_user();
if (!in_array($current_user->ID, $readers)) {
$readers[] = $current_user->ID;
update_post_meta( get_the_ID(), \'post_is_read\', $readers, false);
}
在posts循环中,我添加了以下代码:$readMeta = get_post_meta( get_the_ID(), \'post_is_read\', true );
$current_user = wp_get_current_user();
if ( $readMeta != $current_user->ID ) {
echo \'unread post\';
}
我尝试了第一段代码的许多变体,修改了add\\u post\\u meta、update\\u post\\u meta等。我发现的一个问题是,当打印存储在元键“post\\u is\\u read”中的数组时,输出只是“array”。如果这不是代码中的唯一问题,我认为这是一个主要问题。