[WordPress教程] 免插件实现wordpress调用历史浏览文章列表 -静鱼客栈

2021-09-30 0 610

在网站显示访客的文章浏览历史记录便于访客知道自己阅读过哪些文章,对提高网站的用户体验具有显著的作用。以前介绍过WordPress文章日志浏览历史插件wp-recently-viewed可以实现该功能,但是如果不想通过插件该如何实现?方法是通过提取插件的代码集成到主题中实现。

wordpress调用历史浏览文章列表操作步骤:

1、创建一个history.js文件,文件编码为UTF-8无BOM格式编码,保存在当前主题的js文件夹(没有就创建);

2、在history.js文件中添加下面的代码:

<code>
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: 5,
        storageKey: 'viewHistory',
        primaryKey: 'url'
    });
}
 
var wrap = document.getElementById('recently_viewed');
// 如果 ViewHistory 的实例存在,则可以将页面信息写入。
if(viewHistory) {
    var page = {
        "title": document.getElementsByTagName('title')[0].innerHTML,
        "url": location.href // 这是 primaryKey
        // "time": new Date()
        // "author": ...
        // 这里可以写入更多相关内容作为浏览记录中的信息
    };
    viewHistory.addHistory(page);
} 
// 如果 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 historyItem = histories[i];
 
            var item = document.createElement('li');
            var itemLink = document.createElement('a');
            itemLink.href = historyItem.url;
            itemLink.innerHTML = historyItem.title;
 
            item.appendChild(itemLink);
            list.appendChild(item);
        }
 
        // 插入页面特定位置
        wrap.appendChild(list);
    }
}

3、编辑主题的header.php文件,在</head>前面添加以下代码调用刚刚创建的文件:

<script type="text/javascript" src="<?php bloginfo('template_url'); ?>/js/history.js"></script>

4、在要调用历史浏览文章列表的位置添加代码:

<div id="recently_viewed"></div>

保存文件后,在该位置就会调用自己的浏览历史记录,今天的wordpress调用历史浏览文章列表教程就写到这里了,如果还不会只能去使用插件来满足需求了。

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝/QQ扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

1. 本站所有资源来源于用户上传和网络,因此不包含技术服务请大家谅解!如有侵权请邮件联系客服!305582964@qq.com
2. 本站不保证所提供下载的资源的准确性、安全性和完整性,资源仅供下载学习之用!如有链接无法下载、失效或广告,请联系客服处理,有奖励!
3. 您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容资源!如用于商业或者非法用途,与本站无关,一切后果请用户自负!
4. 如果您也有好的资源或教程,您可以投稿发布,成功分享后有U点奖励和额外收入!

静鱼客栈 学习中心 [WordPress教程] 免插件实现wordpress调用历史浏览文章列表 -静鱼客栈 https://www.52jyu.cn/9662.html

静鱼客栈的帅逼站长~

常见问题
  • 本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP贵宾介绍。
查看详情
  • 最常见的情况是下载不完整: 可对比下载完压缩包的与网盘上的容量,若小于网盘提示的容量则是这个原因。这是浏览器没有下载完整造成的,可以重新进行下载即可~
查看详情

相关文章

发表评论
暂无评论
静鱼客栈-站长

为您解决烦忧 - 专业服务 看到消息会进行回复