wordpress如何获取文章浏览历史记录并显示

633 ℃

wordpress如何获取当前用户的文章浏览历史记录并显示在用户页面里,下面web建站小编给大家详细介绍一下具体实现方法!

具体代码如下:

ViewHistory = function() {
  this.config = {
    limit: 10,
    storageKey: 'viewHistory',
    primaryKey: 'url'
  };

  this.cache = {
    localStorage: null,
    userData: null,
    attr: null
  };
};

ViewHistory.prototype = {

  init: function(config) {
    this.config = config || this.config;
    var _self = this;

    // define localStorage
    if (!window.localStorage && (this.cache.userData = document.body) && 
      this.cache.userData.addBehavior && this.cache.userData.addBehavior('#default#userdata')) {
      this.cache.userData.load((this.cache.attr = 'localStorage'));

      this.cache.localStorage = {
        'getItem': function(key) {
          return _self.cache.userData.getAttribute(key);
        },
        'setItem': function(key, value) {
          _self.cache.userData.setAttribute(key, value);
          _self.cache.userData.save(_self.cache.attr);
        }
      };

    } else {
      this.cache.localStorage = window.localStorage;
    }
  },
  // 保存浏览记录
  addHistory: function(item) {
    var items = this.getHistories();
    for (var i = 0,
    len = items.length; i < len; i++) {
      if (item[this.config.primaryKey] && items[i][this.config.primaryKey] && 
        item[this.config.primaryKey] === items[i][this.config.primaryKey]) {
        items.splice(i, 1);
        break;
      }
    }

    items.push(item);

    if (this.config.limit > 0 && items.length > this.config.limit) {
      items.splice(0, 1);
    }

    var json = JSON.stringify(items);
    this.cache.localStorage.setItem(this.config.storageKey, json);
  },
  // 获取浏览记录
  getHistories: function() {
    var history = this.cache.localStorage.getItem(this.config.storageKey);
    if (history) {
      return JSON.parse(history);
    }
    return [];
  }
};

wordpress主站如何调用子站的文章内容

wordpress如何在页面中获取最新评论文章

wordpress根据tag标签名获取文章列表

标签: wordpress历史记录, wordpress获取文章

上面是“wordpress如何获取文章浏览历史记录并显示”的全面内容,想了解更多关于 wordpress 内容,请继续关注web建站教程。

当前网址:https://m.ipkd.cn/webs_3757.html

声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!

js如何利用正则表达式验证银行卡号
WordPress常见报错问题一
daisyUI如何快速搭建使用
wordpress文章tag标签链接改成id.html(非插件)
利用input做一个颜色选择器即时渲染