早一段时间为亿品元素博客开通了访客足迹功能,现在又想把访客足迹功能功能去除,故发文一篇以记录一下代码,方便以后自己哪天又想折腾的时候,不用满世界找代码。
何谓访客足迹?通俗点的来讲就是把访客自己回复过文章展示出来(有点类似购物网站的浏览历史,不过此处是留言历史而已,并且这一系列文章也只有访客自己能看见,可以说是完善访客体验的一个表现),个人认为,这一功能非常有必要在技术型博客上加载,大家也许会遇到这样的情况,有时去某个技术牛人的博客上留言咨询点问题,但是当你过一段时间去再去看看博主是否已经回复时,却发现自己已经不记得是在哪篇文章中留言了。如果该网站加载了访客足迹功能,那要找回你的评论历史文章就很简单了。只要再在该网站评论一次,你的留言足迹就立即显示出来,非常方便。
下图是亿品元素博客的展示效果,本人是把该功能加载在评论框的下面,其实你可以把这一功能加在其他地方,如sidebar.php
下面是亿品元素上使用的wordpress实现访客足迹功能代码,效果如上图
if($_COOKIE["comment_author_" . COOKIEHASH]!=""){
//1.如果COOKIE不为空,则输出您的足迹
global $wpdb;//2.读取数据库相关项
$sql = "SELECT DISTINCT ID, post_title,post_password,
comment_ID, comment_post_ID,comment_author,
comment_count,
comment_date_gmt,comment_approved, comment_author_email,
comment_type,comment_author_url,
SUBSTRING(comment_content,1,25)
AS com_excerpt FROM $wpdb->comments LEFT OUTER JOIN $wpdb->posts
ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID)
WHERE comment_approved = ‘1’ AND comment_type = ”
AND comment_author = ‘".$_COOKIE["comment_author_" . COOKIEHASH]."’
AND post_password = ”
GROUP BY comment_post_ID
ORDER BY comment_date_gmt
DESC LIMIT 10";//3.输出10篇文章
$comments = $wpdb->get_results($sql);
foreach ($comments as $comment) {
$output .= "\n
ID)."#comment-".$comment->comment_ID. "\" title=\"" . $comment->post_title ."(". $comment->comment_count."评论)\">" . $comment->post_title . "";}//4.输出最近评论过的文章
$output = ‘
’. mb_substr(strip_tags($comment->comment_author),0,12).’ 您最近留下的足迹:
- ’.$output.'
$output = convert_smilies($output);
echo $output;
}
?>