给你,皮特,希望这是;“大团圆结局”;你在想。。。哈哈!
我们要做的第一件事是A)将登录用户的ID记录为post元数据,B)生成一个模板标记,我们可以使用它在某处显示该数据。
第二,我们想把模板标签放到你的单子里。php或显示单条帖子内容的模板部分。
我在整个代码中都做了完整的注释,这样您就可以看到我做了什么,也就不必在这里把它分解成块了。如果你能在一个地方看到整个街区,那就更容易了。
下面的代码是为包含在主题/子主题中而编写的functions.php
文件,但如果您愿意,可以轻松地将其转换为插件。
<?php
// First we run a function in the wp_head or wp_footer where we capture the ID of logged in users and save them to an array in postmeta.
function track_Petes_viewers() {
//If the viewer isn\'t logged in, the rest is pointless, so we check this first.
if( is_user_logged_in() ) :
global $post;
//Lets set a nonce.
$viewers_nonce = wp_create_nonce( \'viewers_nonce_\' . $post->ID );
//Get the current user\'s ID.
$userID = get_current_user_id();
//Lets check if this post already has a list of viewers so we can add to it.
$viewers = get_post_meta( $post->ID, \'petes_viewers\', true );
//If this post doesn\'t already have this meta, lets set up an empty array.
if( empty( $viewers ) ) :
$viewers = array();
endif;
//Now we add the current user\'s ID to the array.
$viewers[] = $userID;
//We don\'t want users being recorded twice, we just need to know if they viewed it, not how may times
//So we\'re going to run array_unique() to make sure each ID is only in there once.
$viewers_list = array_unique( $viewers );
//We verify the nonce and if it works, we update_post_meta(), if not, we kill the process.
if( !wp_verify_nonce( $viewers_nonce, \'viewers_nonce_\' . $post->ID ) ) :
die();
else :
//If the postmeta doesn\'t exist, update_post_meta will create it, so we don\'t need to also use add_post_meta
update_post_meta( $post->ID, \'petes_viewers\', $viewers_list );
endif;
endif;
}
//We\'re going to drop it in the wp_head, but you can change this to a different hook
add_action( \'wp_head\', \'track_Petes_viewers\' );
//Now we generate a template tag that we can drop into our single view of posts, wherever you like, to display a list of viewers
function display_Petes_viewers() {
global $post;
//Not sure what you\'re using this for, but let\'s ONLY show this on posts
if( get_post_type( $post->ID ) == \'post\' ) :
//We get the post meta for viewers
$viewers_list = get_post_meta( $post->ID, \'petes_viewers\', true );
//If there is a record in the DB for this particular post, we\'ll cycle through the IDs and spit out names
if( !empty( $viewers_list ) ) :
//Let\'s wrap the whole thing in a <ul>
echo \'<ul id="petes_viewers">\';
foreach( $viewers_list as $viewer ) :
//We user the ID numbers to get the User object
$viewer_info = get_userdata( (int)$viewer );
//ADDED: Let\'s check if the user still exists.
if( $viewer_info != false ) :
//We pull the display_name from the User object
//You can pull additional info using this pattern, like the email, the link to their profile, their first/lastname, etc.
$viewer_name = $viewer_info->display_name;
//and now we echo out whatever data we pulled from the user object, format this however you like
echo \'<li>\' . $viewer_name . \'</li>\';
endif;
endforeach;
echo \'</ul><!--#petes_viewers-->\';
endif;
endif;
}
?>
您现在有了一个功能,可以在登录用户查看帖子时记录他们的ID。然后还有第二个函数,用于检查此元数据是否存在,如果存在,它将生成一个项目符号列表,列出查看过该特定帖子的用户的显示名称。
要使用第二个函数,只需将以下代码放入所需的主题模板中,即single。php。
<?php display_Petes_viewers(); ?>
现在,有很多事情可以改进。。。比如,我们是否向公众展示了查看该帖子的人员列表?还是只向其他登录用户显示?还是只向管理员或编辑显示?
第二,当我第一次登录并查看帖子时,我的显示名不会显示出来(不是第一次),我们是否想让所有人都喜欢并将当前用户添加到列表中not 已经开始了?
但这些都是相对简单的修改。
希望这有帮助,如果有帮助,请记住接受答案。:-)