typecho通过Cookie实现游客评论记录功能
2个月前
首先声明,下面的代码基本都是用AI写作,博主只是辅助提供意见修改。
其实AI提供了两个版本实现下面的功能,(第一个为数据库版本(为这个小功能使用数据库感觉不值得),第二就行下面这个)。
注意以下第一步和第二步代码可以放到functions.php这个文件中(注意:最好放到文件末尾避免)
第一步添加浏览记录代码
第二步添加详情页面插件钩子挂载点(array('xm', 'addHistory')通过这个数组传递上面第一步定义的类,xm是上面类的名称,addHistory是上面类中的方法)
Typecho_Plugin::factory('Widget_Feedback')->comment = array('xm', 'logComment');第三步,就是在你需要显示的地方调用下面代码。(其中的样式根据自己主题风格自行修改)
<?php
$comment = Typecho_Cookie::get('__comment_history');
$comment = $comment ? json_decode($comment, true) : array();
if ($comment) {
?>
<div class="xm-zuji">
<?php foreach ($comment as $item) {
echo '<div class="xm-zuji-div"><span class="xm-zuji-time xm-txt-ellipsis">'.$item['author'].'</span><span class="xm-zuji-a xm-txt-ellipsis">'.xm::time($item['created']).'评论了:'.xm::biaoqing($item['text']).'</span></div>';
}
?>
</div>
<?php }else{echo '<div class="xm-zuji-xx">暂无过往的评论消息</div>';} ?>
困告小周
2个月前 . LV.0
小天
2个月前 . LV.0
来看看好不好用