前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Typecho通用评论组件包含Css样式

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

作者头像
泽泽社长
发布2023-10-01 11:21:16
3820
发布2023-10-01 11:21:16
举报
文章被收录于专栏:泽泽社

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

追加函数

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

代码语言: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样式代码如下

代码语言:css
复制
/*通用评论组件样式草案*/
#comments a {
    color: #3F51B5;
}
#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 .comment-reply a{
    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 button {
    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 button: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%;
    border-radius: 5px;
    padding: 4px 8px;
}
@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文件中填入如下代码,然后在文章页面中引用这个文件即可

代码语言:html
复制
<?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';
            }
        }
        ?>
        <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');
        echo $commentClass;
        ?>">
            <div class="comment-col" id="<?php $comments->theId(); ?>">
            <div class="comment-author" itemprop="creator" itemscope itemtype="http://schema.org/Person">
                <span
                    itemprop="image">
                    <?php $comments->gravatar(50); ?>
                </span>
                <cite class="fn" itemprop="name"><?php $comments->author(); ?></cite>
                <div class="comment-reply">
                <?php $comments->reply('回复'); ?>
                </div>
            </div>
            <div class="comment-meta">
                <a href="<?php $comments->permalink(); ?>">
                    <time itemprop="commentTime"
                          datetime="<?php $comments->date('c'); ?>">
                          <?php $comments->date('Y年m月d日'); ?></time>
                </a>
                <?php if ('waiting' == $comments->status) { ?>
                    <em class="comment-awaiting-moderation">您的评论正等待审核!</em>
                <?php } ?>
            </div>
            <div class="comment-content" itemprop="commentText">
<?php
    $content = preg_replace('/<p>(.*)/', '<p>'.get_comment_at($comments->coid).'$1', $comments->content);
    echo $content;
?>
            </div>
            </div>
            <?php if ($comments->children) { ?>
                <div class="comment-children" itemprop="discusses">
                    <?php $comments->threadedComments(); ?>
                </div>
            <?php } ?>
        </li>
        <?php
    }
?>

<div id="comments">
    <?php $this->comments()->to($comments); ?>

    <?php if ($this->allow('comment')): ?>
        <div id="<?php $this->respondId(); ?>" class="respond">
            <div class="cancel-comment-reply">
                <?php $comments->cancelReply(); ?>
            </div>

            <h3 id="response"><?php _e('添加新评论'); ?></h3>
            <form method="post" action="<?php $this->commentUrl() ?>" id="comment-form" role="form">
                <?php if ($this->user->hasLogin()): ?>
                    <p><?php _e('登录身份: '); ?><a
                            href="<?php $this->options->profileUrl(); ?>"><?php $this->user->screenName(); ?></a>. <a
                            href="<?php $this->options->logoutUrl(); ?>" title="Logout"><?php _e('退出'); ?> &raquo;</a>
                    </p>
                <?php else: ?>
                <div class="inputgrap">
                    <p>
                        <label for="author" class="required"><?php _e('称呼'); ?></label>
                        <input type="text" name="author" id="author" class="text"
                               value="<?php $this->remember('author'); ?>" required/>
                    </p>
                    <p>
                        <label
                            for="mail"<?php if ($this->options->commentsRequireMail): ?> class="required"<?php endif; ?>><?php _e('Email'); ?></label>
                        <input type="email" name="mail" id="mail" class="text"
                               value="<?php $this->remember('mail'); ?>"<?php if ($this->options->commentsRequireMail): ?> required<?php endif; ?> />
                    </p>
                    <p>
                        <label
                            for="url"<?php if ($this->options->commentsRequireURL): ?> class="required"<?php endif; ?>><?php _e('网站'); ?></label>
                        <input type="url" name="url" id="url" class="text" placeholder="<?php _e('http://'); ?>"
                               value="<?php $this->remember('url'); ?>"<?php if ($this->options->commentsRequireURL): ?> required<?php endif; ?> />
                    </p>
                </div>
                <?php endif; ?>
                <p>
                    <label for="textarea" class="required"><?php _e('内容'); ?></label>
                    <textarea rows="8" cols="50" name="text" id="textarea" class="textarea"
                              required><?php $this->remember('text'); ?></textarea>
                </p>
                <p>
                    <button type="submit" class="submit"><?php _e('提交评论'); ?></button>
                    <div class="comment-clear"></div>
                </p>
            </form>
        </div>
    <?php else: ?>
        <h3><?php _e('评论已关闭'); ?></h3>
    <?php endif; ?>
    
<?php if ($comments->have()): ?>
        <h3><?php $this->commentsNum(_t('暂无评论'), _t('仅有一条评论'), _t('已有 %d 条评论')); ?></h3>

<?php $comments->listComments(); ?>
        <div class="comment-pagegroup">
<?php

$npattern = '/\<li.*?class=\"next\"><a.*?\shref\=\"(.*?)\"[^>]*>/i';
$ppattern = '/\<li.*?class=\"prev\"><a.*?\shref\=\"(.*?)\"[^>]*>/i';
ob_start();
$comments->pageNav();
$con = ob_get_clean();
$n=preg_match_all($npattern, $con, $nextlink);
$p=preg_match_all($ppattern, $con, $prevlink);
if($n){
$nextlink=$nextlink[1][0];
$nextlink=str_replace("#comments","?type=comments",$nextlink);
}else{
$nextlink=false;
}

if($p){
$prevlink=$prevlink[1][0];
$prevlink=str_replace("#comments","?type=comments",$prevlink);
}else{
$prevlink=false;
}
?>
<?php if($prevlink): ?>
    <a href="<?php echo $prevlink; ?>" class="content-page">
            <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M7 16l-4-4m0 0l4-4m-4 4h18" />
            </svg>
            <span>
                上一页
            </span>
    </a>
<?php else: ?>
<div></div>
<?php endif; ?>
<?php if($nextlink): ?>
    <a href="<?php echo $nextlink; ?>" class="content-page">
            <span>
                下一页
            </span>
            <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17 8l4 4m0 0l-4 4m4-4H3" />
            </svg>
    </a>
<?php endif; ?>

    </div>
<?php endif; ?>

</div>

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系转载前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 追加函数
  • css样式
  • 评论组件
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档