调用访客最近浏览过的文章

quality,Q 70

1638704608 20211205114328 61aca5e06d270
1638704608 20211205114328 61aca5e072191

WordPress最近浏览过的文章

调用最近浏览过的文章,比较蛋疼的功能,原理是通过读取浏览器cookie文件,调用显示最近(360天)被访问过的10篇文章 ,需要注意的是,该功能不是统计所有浏览者最近查看过的文章,并没有写进数据库中,每个访客都有自己独有的浏览清单。

将下面代码添加到主题functions.php文件的最后:

  1. <?php
  2. /* 可选参数: */
  3. $zg_cookie_expire = 360; // cookie过期时间,默认值是360天
  4. $zg_number_of_posts = 10; // 显示篇数,默认值是10。
  5. $zg_recognize_pages = true;
  6. /* 此行后不要编辑 */
  7. function zg_lwp_header() {
  8. if (is_single()) {
  9. zg_lw_setcookie();
  10. } elseif (is_page()) {
  11. global$zg_recognize_pages;
  12. if ($zg_recognize_pages === true) {
  13. zg_lw_setcookie();
  14. }
  15. }
  16. }
  17. function zg_lw_setcookie() {
  18. global$wp_query;
  19. $zg_post_ID = $wp_query->post->ID;
  20. if (! isset($_COOKIE[“WP-LastViewedPosts”])) {
  21. $zg_cookiearray = array($zg_post_ID);
  22. } else {
  23. $zg_cookiearray = unserialize(preg_replace(‘!s:(\d+):“(.*?)”;!e’, “‘s:’.strlen(‘$2′).’:\”$2\”;'”, stripslashes($_COOKIE[“WP-LastViewedPosts”])));
  24. if (! is_array($zg_cookiearray)) {
  25. $zg_cookiearray = array($zg_post_ID);
  26. }
  27. }
  28. if (in_array($zg_post_ID, $zg_cookiearray)) {
  29. $zg_key = array_search($zg_post_ID, $zg_cookiearray);
  30. array_splice($zg_cookiearray, $zg_key, 1);
  31. }
  32. array_unshift($zg_cookiearray, $zg_post_ID);
  33. global$zg_number_of_posts;
  34. while (count($zg_cookiearray) > $zg_number_of_posts) {
  35. array_pop($zg_cookiearray);
  36. }
  37. $zg_blog_url_array = parse_url(get_bloginfo(‘url’));
  38. $zg_blog_url = $zg_blog_url_array[‘host’];
  39. $zg_blog_url = str_replace(‘www.’, , $zg_blog_url);
  40. $zg_blog_url_dot = ‘.’;
  41. $zg_blog_url_dot .= $zg_blog_url;
  42. $zg_path_url = $zg_blog_url_array[‘path’];
  43. $zg_path_url_slash = ‘/’;
  44. $zg_path_url .= $zg_path_url_slash;
  45. global$zg_cookie_expire;
  46. setcookie(“WP-LastViewedPosts”, serialize($zg_cookiearray), (time()+($zg_cookie_expire*86400)), $zg_path_url, $zg_blog_url_dot, 0);
  47. }
  48. function zg_recently_viewed() {
  49. echo ‘<ul class=“viewed_posts”>’;
  50. if (isset($_COOKIE[“WP-LastViewedPosts”])) {
  51. $zg_post_IDs = unserialize(preg_replace(‘!s:(\d+):“(.*?)”;!e’, “‘s:’.strlen(‘$2′).’:\”$2\”;'”, stripslashes($_COOKIE[“WP-LastViewedPosts”])));
  52. foreach ($zg_post_IDsas$value) {
  53. global$wpdb;
  54. $zg_get_title = $wpdb->get_results(“SELECT post_title FROM $wpdb->posts WHERE ID = ‘$value+0’ LIMIT 1”);
  55. foreach($zg_get_titleas$zg_title_out) {
  56. echo“<li><a href=\””. get_permalink($value+0) . “\” title=\””. $zg_title_out->post_title . “\”>”. $zg_title_out->post_title . “</a></li>\n”;
  57. }
  58. }
  59. } else {
  60. }
  61. echo ‘</ul>’;
  62. }
  63. add_action(‘get_header’,’zg_lwp_header’);
  64. ?>

由于代码高亮转换后代码可能执行有误,这里提供 源文件下载

调用方法,在侧边sidebar模版或者正文single模版适当位置加入下面代码;

  1. <?php if (function_exists(‘zg_recently_viewed’)): if (isset($_COOKIE[“WP-LastViewedPosts”])) { ?>
  2. <h2>Last viewed posts</h2>
  3. <?php zg_recently_viewed(); ?>
  4. <?php } endif; ?>

 

注:调用并不十分准确,有一定误差,效果看本站正文最后。

最后说明一下,代码提取自:Last Viewed Posts 插件,如果嫌折腾麻烦,可以直接安装并启用该插件,使用更为方便,可以直接将该功能拖到侧边小工具中。

 

类似文章