這是用js實現WordPress文章遊覽歷史功能。
和上一篇代碼不同。可根據個人喜好而使用。
首先,js代碼是來自author: mg12
製作一個js命名是view-history.js
或將以下代碼貼入已經調動的js裡面

  [cc lang="php"] /** * @author: mg12 * @update: 2013/01/09 * * IE6/7 need a third-party JSON library to polyfill this feature. [https://github.com/douglascrockford/JSON-js/blob/master/json2.js] */ 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 []; } }; //初始化 if (typeof localStorage !== 'undefined' && typeof JSON !== 'undefined') { var viewHistory = new ViewHistory(); viewHistory.init({ limit: 8, storeagekey: 'viewHistory', primaryKey: 'url' }); } //保存頁面資訊 如果 ViewHistory 的實例存在,則可以將頁面資訊寫入。 if (viewHistory) { var page = { "title": document.getElementsByTagName('title')[0].innerHTML, "url": location.href // 這是 primaryKey // "time": new Date() // "author": ... // 這裡可以寫入更多相關內容作為流覽記錄中的資訊 // 截斷的辦法:"title": document.getElementsByTagName('title')[0].innerHTML.split(" | ")[0] }; viewHistory.addHistory(page); } var wrap = document.getElementById('view-history'); // 如果 ViewHistory 的實例存在,並且外層節點存在,則可顯示歷史流覽記錄 if (viewHistory && wrap) { // 獲取流覽記錄 var histories = viewHistory.getHistories(); // 組裝列表 var list = document.createElement('ul'); if (histories && histories.length > 0) { for (var i = histories.length - 1; i >= 0; i--) { var history = histories[i]; var item = document.createElement('li'); var link = document.createElement('a'); link.href = history.url; link.innerHTML = history.title; item.appendChild(link); list.appendChild(item); } // 插入頁面特定位置 wrap.appendChild(list); } } [/cc]  

調動此js檔案,若是已經貼入其他的js檔,則可省略。
在header.php的及...裡面貼入下面代碼.

[cc lang="php"]

[/cc]

最後,在你需要使用文章遊覽記錄的地方,
 
貼入下面代碼調動

[cc lang="php"]

 

[/cc]

如果你只想要顯示文章歷史,
 
不想顯示頁面遊覽歷史。
 
可以在header.php的及...裡面換成下面代碼.

 [cc lang="php"]

[/cc]

相關閱讀