获取访客的历史评论

559天前2048

前两天在Kam 博客上看到有个小铃铛,如果你是已评论或者登录状态,点击可以看到你之前在他博客的评论,想着好像可以干些什么,hah~

在网上找到了一个 typecho 类似功能的,然后改了改,目前实现用户在 cookie 记住邮箱的前提下,调用出用户之前评论过的内容,还不错。

当然,这里还要感谢Kiosr教我的语法调用,嘿嘿。

修改后的代码如下(原文链接):

在 function 里加入:
class Widget_Comments_RecentPlus extends Widget_Abstract_Comments
{
    public function __construct($request, $response, $params = NULL)
    {
        parent::__construct($request, $response, $params);
        $this->parameter->setDefault(array('pageSize' => $this->options->commentsListSize, 'parentId' => 0, 'ignoreAuthor' => false));
    }
    public function execute()
    {
        $select  = $this->select()->limit($this->parameter->pageSize)
        ->where('table.comments.status = ?', 'approved')
        ->order('table.comments.coid', Typecho_Db::SORT_DESC);
        if ($this->parameter->parentId) {
            $select->where('cid = ?', $this->parameter->parentId);
        }
        if ($this->options->commentsShowCommentOnly) {
            $select->where('type = ?', 'comment');
        }
        /** 忽略作者评论 */
        if ($this->parameter->ignoreAuthor) {
            $select->where('ownerId <> authorId');
        }
        if ($this->parameter->mail) {
            $select->where('mail = ?', $this->parameter->mail);
        }
        $this->db->fetchAll($select, array($this, 'push'));
    }
}

然后在需要的地方调用即可:

<?php if ($this->remember('mail',true) != "") :?>
  <?php $mail=Typecho_Cookie::get('__typecho_remember_mail'); $this->widget('Widget_Comments_RecentPlus', 'mail='.$mail)->to($comments);?>
  <?php while($comments->next()): ?>
    <li><a href="<?php $comments->permalink(); ?>"><?php strip_tags($comments->excerpt(99, '..')); ?></a></li>
  <?php endwhile; ?>
<?php endif;?>

不过这只是简单的调出了用户历史评论,至于弄这玩意有个啥用,先按下不表,可以发挥想象力做一些有趣的小功能,挖个坑,以后慢慢再填,哈哈。

* 若非特殊说明,本站文章均为博主原创,码字不易,如需转载,请注明出处!有疑问可留言交流,谢谢。

PHPTypecho分享16 

获取访客的历史评论 - Jdeal | Life is like a Design.