今天给博客实现了一个比较有意思的小功能,如图所示:

可以在博客首页显示输出一个类似说说的功能。

前端

前端样式的实现参考(fu zhi)了站点OFFODD?,具体代码如下:

<article class="post whisper">
    <h2 class="post-title">
        <a href="#">
            Yacan Man 
            <small>2022-11-11</small>
            <span class="more">···</span>
        </a>
    </h2>
    <div class="post-content">
        <p>比成功更宝贵的,是敢于挑战不可能的勇气。</p>
    </div>
</article>

css样式为:

.post.whisper {
        min-height: 40px;
        margin: 1.5em 0;
        padding: 0 1em;
        text-align: center;
        border: 1px solid #444;
        border-radius: 2px;
    }
.whisper .post-title {
    font-size: 1em;
    line-height: 1.5;
    font-weight: normal;
    margin: 0 -1em;
}
.whisper.post .post-title a {
    display: block;
    padding: 2px 1em;
    color: #fff;
    background: #444;
}
.whisper .post-title .more {
    position: relative;
    float: right;
}

后端

本博客系统的矫情独立页面评论区是我自己的一个日常说说,那么我后端实现的需求就是如何从这个独立页面获取最新的评论。

了解了需求,接下来就是上代码,感谢大佬泽泽社长的帮助!大佬就是厉害,一顿操作猛如虎!

如何只在博客首页输出:

<?php if($this->currentPage==1||$this->_currentPage==1): ?>  
    // 输出内容
<?php endif; ?>

如何输出某个页面的最新n条评论,考虑到是在首页输出,因此不输出评论中的图片:

<?php $this->widget('Widget_Comments_Recent', 'pageSize=1&parentId=1734')->to($comments); ?>
    <!--pageSiz获取的评论条数,parentId=cid;&ignoreAuthor=true忽略文章作者评论-->
    <?php if ($comments->have()): ?>
    <?php while($comments->next()): ?>
        <?php $comments->author(false); ?> // 输出作者
        <?php $comments->date('Y-m-d'); ?> // 输出评论时间
        <?php $comments->excerpt(30, '...[点击标题展开]'); ?> // 输出评论中的文字的前30个字符,
        // $comments->content(); 可以输出评论中的所有内容
      <?php endwhile; ?>
    <?php endif; ?>

接下来就是如何排除特殊情况,从输出的评论中过滤掉嵌套评论与非博主评论,需要修改文件var/Widget/Comments/Recent.php,在第45行后面添加:

->where('table.comments.authorId = ?', '1') // 过滤博主评论
->where('table.comments.parent = ?', '0')  // 过滤非嵌套评论

修改示意
修改示意

这两行代码以后会不会影响到那些侧边栏调用最新评论的博客程序?我自己没有尝试,如果加了代码后侧边栏最新评论出现问题需要自己再进行调整!后面已更新,完美解决!

更新

解决上面遗留的问题,大佬泽泽社长给出了解决方案,如何在不影响侧边栏调用最新评论的基础上,对说说中仅输出博主的非嵌套最新评论。

1、在上述文件需要修改文件var/Widget/Comments/Recent.php那里,对文件的修改改为在第58行插入:

if ($this->parameter->parentnum) {
    $select->where('table.comments.parent = ?', $this->parameter->parentnum);
}
if ($this->parameter->authorId) {
    $select->where('table.comments.authorId = ?', $this->parameter->authorId);
}

这个时候之前的调用参数也需要进行调整:

<?php $this->widget('Widget_Comments_Recent', 'pageSize=评论条数&parentId=文章cid&parentnum=0&authorId=作者uid')->to($comments); ?>

完成以上修改可以完美避开博客主题侧边栏调用最新评论也过滤掉非博主评论以及嵌套评论的问题。