思路:使用Ajax自动提交请求增加文章阅读数,Cookie记录是否已经提交,避免刷新重复提交。 JS代码(保存为views.js): //获取Cookie function getCookie(name) { var str = document.cookie; var arr = str.split('; '); for(var i=0; i<arr.length; i++) { var c = arr[i].split('='); if(c[0] == name) { return c[1]; } } return false; } //Ajax请求 function setPostmeta(varname, postid, target) { var cookie = getCookie(varname); if(cookie) return; jQuery.ajax({ cache: false, url: ajax_object.ajax_url, type: 'POST', data: { 'action': 'add_views', 'post_id': postid, }, dataType: 'JSON', async: true, success:function(result) { if(result.status == 'success') { if(target) target.text(result.views); var exdate = new Date(); exdate.setDate(exdate.getDate() + 360); document.cookie = varname + '=' + result.views + '; expires=' + exdate.toGMTString() + '; path=/'; } } }); } functions.php中添加如下代码: …