Typecho通用评论组件包含Css样式

2026年04月08日 作者头像 作者头像 站长 编辑

一直以来我都不太喜欢写Typecho评论列表样式,那时候多说还没有死,就想着有没有大佬直接写好一套Typecho评论列表简单样式,然后别人就可以改改csshtml结构就能diy自己的样式,不用考虑php啥的,于是乎昨天简单弄了下。

【202401:调整了下css,修复了下翻页按钮翻页后不跳转评论区的问题】

追加函数

functions.php中加入如下两个函数,第一个函数是强制设置一些参数,其中楼层限制可根据需求改成自己需要的数量,第二个函数是评论@上级评论的功能。

function themeInit($archive)
{
// 强制用户文章最新评论显示在文章首页
 Helper::options()->commentsPageDisplay = 'first';
// 将较新的评论显示在前面
 Helper::options()->commentsOrder= 'DESC';
// 突破评论回复楼层限制
 Helper::options()->commentsMaxNestingLevels = 999;
}
function get_comment_at($coid)
{//评论@函数
    $db   = Typecho_Db::get();
    $prow = $db->fetchRow($db->select('parent')->from('table.comments')
                                 ->where('coid = ?', $coid));
    $parent = $prow['parent'];
    if (!empty($parent)) {
        $arow = $db->fetchRow($db->select('author')->from('table.comments')
                                     ->where('coid = ? AND status = ?', $parent, 'approved'));
if(!empty($arow['author'])){ $author = $arow['author'];
        $href   = '<a href="#comment-' . $parent . '">@' . $author . '</a> ';
        return $href;
}else { return '';}
    } else {
        return '';
    }
}

css样式

评论css样式代码如下

/*通用评论组件样式草案*/
#comments a {
    color: #3F51B5;
}
#comments li, #comments ol {
    list-style: none;
    margin: 0;
    padding: 0;
}
#comments .comment-pagegroup {
    display: flex;
    justify-content: space-between;
    margin: 20px 0;
}
#comments a.content-page {
    color: #fff;
    background: #3F51B5;
    padding: 5px 10px;
    font-size: 14px;
    display: flex;
    align-items: center;
    border-radius: 8px;
    transition-property: all;
    transition-timing-function: cubic-bezier(0.4, 0, 0.2, 1);
    transition-delay: 100ms;
}
#comments a.content-page:hover {
    background: #000;
}
#comments .content-page svg {
    width: 20px;
    height: 20px;
    display: block;
    vertical-align: middle;
}
#comments img.avatar {
    float: left;
    margin-right: 10px;
    border: 2px solid #e6e6e6;
    border-radius: 5px;
}
.comment-list .comment-col {
    padding: 10px;
    margin: 10px 0;
    border-bottom: 1px solid #dfdfdf;
    background: rgba(222,222,222,.3);
    border-radius: 8px;
}
.comment-author cite {
    font-weight: bold;
    font-style: normal;
}
#comments .comment-meta a {
    color: #999;
    font-size: .92857em;
}
#comments .comment-reply{
    float: right;
    font-weight: bold;
}
#comments .cancel-comment-reply button, #comments .comment-reply button {
    color: #3F51B5;
    font-size: small;
}
#comments input,.protected input{
    z-index: 10;
    padding: 5px;
    border-radius: 5px;
    border: 0;
    -webkit-box-shadow: 0 0 35px 0 rgba(154,161,171,.15);
    box-shadow: 0 0 35px 0 rgba(154,161,171,.15);
}
#comments p {
    margin-bottom: 8px;
}
#comments label {
    color: #3F51B5;
    font-size: small;
}
#comments .submit {
    background: #2196F3;
    color: white;
    padding: 5px 10px;
    font-size: 14px;
    border-radius: 5px;
    float: right;
    transition-property: all;
    transition-timing-function: cubic-bezier(0.4, 0, 0.2, 1);
    transition-delay: 100ms;
}
#comments .submit:hover {
    background: #3F51B5;
}
/*【添加评论】文字样式*/
#comments #response {
    font-size: 18px;
    font-weight: bold;
}
#comments .comment-body #response {
    display: none;
}
/*【评论框】文字样式*/
#comments #textarea {
    display: block;
    border: 1px solid #c2c2c2;
    width: 100%;
    box-sizing: border-box;
    border-radius: 5px;
    padding: 4px 8px;
    min-height: 100px;
}
@media (min-width: 768px){
#comments .inputgrap {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 1rem;
}
}
#comments input {
    display: block;
    border: 1px solid #c2c2c2;
    width: 100%;
    border-radius: 5px;
    padding: 4px 8px;
}
/*二级评论缩进*/
.comment-children {
    margin-left: 25px;
}
.comment-children .comment-children {
    margin-left: 0;
}
.comment-clear{
    clear: both;
    height: 0;
    line-height: 0;
    font-size: 0;
}

评论组件

comments.php文件中填入如下代码,然后在文章页面中引用这个文件即可

<?php if (!defined('__TYPECHO_ROOT_DIR__')) exit; 
//如果你想使用其他评论头像插件,请注释下面这行代码!
define('__TYPECHO_GRAVATAR_PREFIX__', 'https://cravatar.cn/avatar/');
?>
<?php function threadedComments($comments, $options){
 $commentClass = '';
        if ($comments->authorId) {
            if ($comments->authorId == $comments->ownerId) {
                $commentClass .= ' comment-by-author';
            } else {
                $commentClass .= ' comment-by-user';
            }
        }

   if ($comments->url) {
        $author = '<a href="' . $comments->url . '" target="_blank" rel="external nofollow">' . $comments->author . '</a>';
    } else {
        $author = $comments->author;
    }
        ?>
        <li itemscope itemtype="http://schema.org/UserComments" class="comment-body<?php
        if ($comments->levels > 0) {
            echo ' comment-child';
            $comments->levelsAlt(' comment-level-odd', ' comment-level-even');
        } else {
            echo ' comment-parent';
        }
        $comments->alt(' comment-odd', ' comment-even');