此插件可能满足您的要求。
http://wordpress.org/extend/plugins/recently-viewed-posts
用法
get_recently_viewed_posts( $max_shown = 10 )
返回li的字符串。recently_viewed_posts( $max_shown = 10 )
打印div
如果您有兴趣学习如何自己开发此功能,请下载插件并查看源文件。WordPress是开源的!
更新:
上述插件无法工作。以下方法将有助于设置最近查看的视频ID。
<?php
/*
* Plugin Name: WPSE_63266_Recently_Viewed
*/
function wpse_63266_update_recently_viewed(){
/**
* If is admin or isn\'t single, then return.
* To get only singular video posts use; if(!is_singular(\'videos\')) return;
*/
if(is_admin() || !is_single()) return;
global $post;
// Get the current post id.
$current_post_id = get_the_ID();
if(is_user_logged_in()){
// Store recently viewed post ids in user meta.
$recenty_viewed = get_user_meta(get_current_user_id(), \'recently_viewed\', true);
if( \'\' == $recenty_viewed ){
$recenty_viewed = array();
}
// Prepend id to the beginning of recently viewed id array.(http://php.net/manual/en/function.array-unshift.php)
array_unshift($recenty_viewed, $current_post_id);
// Keep the recently viewed items at 5. (http://www.php.net/manual/en/function.array-slice.php)
$recenty_viewed = array_slice($recenty_viewed, 0, 5); // Extract a slice of the array
// Update the user meta with new value.
update_user_meta(get_current_user_id(), \'recently_viewed\', $recenty_viewed);
} else {
/**
* For non-logged in users you can use the same procedure as above
* using get_option() and update_option()
*/
}
}
add_action(\'wp_footer\', \'wpse_63266_update_recently_viewed\');
function wpse_63266_show_recently_viewed(){
$recenty_viewed = get_user_meta(get_current_user_id(), \'recently_viewed\', true);
echo \'<pre>\'; print_r($recenty_viewed); echo \'</pre>\';
}
add_action(\'wpse_63266_recently_viewed\', \'wpse_63266_show_recently_viewed\');
?>
do action()
在模板中查看在浏览帖子时数组值(视频ID)的更改。
do_action(\'wpse_63266_recently_viewed\');