wordpress实现用户历史阅读记录功能分享
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>COOKIE记录<br>
SESSION记录<br>
数据库记录<br>
缓存文件记录<br>
从技术难度上,第1、2种方法最简单,因此,下文将实现以COOKIE为记录方法的实现代码。其中第1、2、4种方法思路基本相同,即通过在用户访问某个页面时,通过对某个特定名称的COOKIE或SESSION或缓存文件中增加值,把当前URL或唯一标识码记录其中,而在使用时,只需要调用该名称的COOKIE或SESSION或缓存文件中的值,并把对应的项目链接列出即可。</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
而数据库记录方法一般需要用户先注册,注册之后记录过程同上,在访问页面时,将这个URL或页面对应的唯一标识码存入用户ID对应的某个数据库字段或表中,在使用时,通过数据库查询把这些值调用出来即可。这种方法不同于前一种,存入数据库的记录是永久不会丢失的,不会由于COOKIE或缓存过期而丢失,也可以实现跨终端,即在不同的电脑上也可以调用。</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
在wordpress中,我们通过一些钩子来实现访问文章时把该文章的ID记录下来。</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
</p>
<div style='margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden; font-family: tahoma, arial, "Microsoft YaHei";'>
<div style="margin: 0px; padding: 0px; outline: none; float: right; line-height: 25.2px; font-size: 14px;">
<span style="line-height: 25.2px; cursor: pointer;"><u>复制代码</u></span>
</div>
代码如下:</div>
<div id="phpcode8" style='margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; font-size: 14px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word; font-family: tahoma, arial, "Microsoft YaHei";'>
<br>
add_action('wp','HistoryReadCookieInit',10);<br>
function HistoryReadCookieInit(){<br>
if(is_single()){<br>
global $post;<br>
if(isset($_COOKIE['history-read-posts']) && !empty($_COOKIE['history-read-posts']))$history_read_posts = trim($_COOKIE['history-read-posts']);<br>
else $history_read_posts = '';<br>
$post_id = $post->ID;<br>
if(strpos($history_read_posts,$post_id.',') === 0){<br>
$history_read_posts = str_replace($post_id.',','',$history_read_posts);<br>
}elseif(strpos($history_read_posts,$post_id.',') > 0){<br>
$history_read_posts = str_replace(','.$post_id.',',',',$history_read_posts);<br>
}<br>
$history_read_posts = $post_id.','.$history_read_posts;<br>
setcookie('history-read-posts',$history_read_posts,time()+315360000,COOKIEPATH,COOKIE_DOMAIN,false);<br>
}<br>
}</div>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
在记录的时候,我们需要对已经被记录的文章ID做一个简单的运算,保证COOKIE中只有一个ID,不重复。并按照我们访问的时间来对这些文章ID排序(最后访问的排在最前)。</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
在使用的时候,我们使用下面的代码,直接调用这些被记录的ID即可。</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
</p>
<div style='margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden; font-family: tahoma, arial, "Microsoft YaHei";'>
<div style="margin: 0px; padding: 0px; outline: none; float: right; line-height: 25.2px; font-size: 14px;">
<span style="line-height: 25.2px; cursor: pointer;"><u>复制代码</u></span>
</div>
代码如下:</div>
<div id="phpcode9" style='margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; font-size: 14px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word; font-family: tahoma, arial, "Microsoft YaHei";'>
<br>
if(!isset($_COOKIE['history-read-posts']) || empty($_COOKIE['history-read-posts']))return;<br>
$history_read_posts = trim($_COOKIE['history-read-posts']);<br>
$history_read_posts = array_unique(array_filter(explode(',',$history_read_posts,$count)));<br>
foreach($history_read_posts as $key => $p){<br>
$get_post = get_post($p);<br>
echo '<li><a href="'.get_permalink($p).'" title="'.get_the_title($p).'" target="_blank">'.get_the_title($p).'</a></li>';<br>
}</div>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
这种方法最直接有效,而且理解起来非常容易。</p>
頁:
[1]