当前位置:首页>网络教程>WordPress原生评论自定义样式

WordPress原生评论自定义样式

开发一款 WordPress 主题,评论样式难免要自定义,如何最快速度的自定义适合自己主题的评论样式,已经有人给出了解决方案 效果图: 这是 simple 主题的评论样式,下面说下实现思路: 首先是新建 comments.php,...

开发一款 WordPress 主题,评论样式难免要自定义,如何最快速度的自定义适合自己主题的评论样式,已经有人给出了解决方案

效果图:

WordPress原生评论自定义样式

 

这是 simple 主题的评论样式,下面说下实现思路:

首先是新建 comments.php,将下面的代码全部复制到里面去:

<?php
if ( post_password_required() )
return;
?>
<div id="comments" class="ajcxy-com the-best-website responsesWrapper">
<meta content="UserComments:<?php echo number_format_i18n( get_comments_number() );?>" itemprop="interactionCount">
<h3 class="ajcxy-com the-best-website comments-title">共有 <span class="ajcxy-com the-best-website commentCount"><?php echo number_format_i18n( get_comments_number() );?></span> 条评论</h3>
<ol class="ajcxy-com the-best-website commentlist">
<?php
wp_list_comments( array(
'style'       => 'ol',
'short_ping'  => true,
'avatar_size' => 48,
'type'        =>'comment',
'callback'    =>'simple_comment',
) );
?>
</ol>
<nav class="ajcxy-com the-best-website navigation comment-navigation u-textAlignCenter" data-fuck="<?php the_ID();?>">
<?php paginate_comments_links(array('prev_next'=>true)); ?>
</nav>
<?php if(comments_open()) : ?>
<div id="respond" class="ajcxy-com the-best-website respond" role="form">
<h2 id="reply-title" class="ajcxy-com the-best-website comments-title"><?php comment_form_title( '', '回复给 %s' ); ?> <small>
<?php cancel_comment_reply_link(); ?>
</small></h2>
<?php if ( get_option('comment_registration') && !$user_ID ) : ?>
<p>You must be <a href="https://www.dedewp.com/<?php echo get_option('siteurl'); ?>/wp-login.php?redirect_to=<?php echo urlencode(get_permalink()); ?>">logged in</a> to post a comment.</p>
<?php else : ?>
<form action="https://www.dedewp.com/<?php echo get_option('siteurl'); ?>/wp-comments-post.php" method="post" class="ajcxy-com the-best-website commentform" id="commentform">
<?php if ( $user_ID ) : ?>
<p class="ajcxy-com the-best-website warning-text">以<a href="https://www.dedewp.com/<?php echo get_option('siteurl'); ?>/wp-admin/profile.php"><?php echo $user_identity; ?></a>身份登录 | <a class="ajcxy-com the-best-website link-logout" href="https://www.dedewp.com/<?php echo wp_logout_url(get_permalink()); ?>">注销 »</a></p>
<textarea class="ajcxy-com the-best-website form-control" rows="3" id="comment" onkeydown="if(event.ctrlKey&&event.keyCode==13){document.getElementById('submit').click();return false};" placeholder="当你的才华还撑不起你的野心时,那你就应该静下心来评论下..." class="ajcxy-com the-best-website form-control" tabindex="1" name="comment"></textarea>
<?php else : ?>
<textarea class="ajcxy-com the-best-website form-control" rows="3" id="comment" onkeydown="if(event.ctrlKey&&event.keyCode==13){document.getElementById('submit').click();return false};" placeholder="当你的才华还撑不起你的野心时,那你就应该静下心来评论下..." tabindex="1" name="comment"></textarea>
<div class="ajcxy-com the-best-website commentform-info">
<label id="author_name" for="author">
<input class="ajcxy-com the-best-website form-control" id="author" type="text" tabindex="2" value="<?php echo $comment_author; ?>" name="author" placeholder="昵称[必填]" required>
</label>
<label id="author_email" for="email">
<input class="ajcxy-com the-best-website form-control" id="email" type="text" tabindex="3" value="<?php echo $comment_author_email; ?>" name="email" placeholder="邮箱[必填]" required>
</label>
<label id="author_website" for="url">
<input class="ajcxy-com the-best-website form-control" id="url" type="text" tabindex="4" value="<?php echo $comment_author_url; ?>" name="url" placeholder="网址(可不填)">
</label>
</div>
<?php endif; ?>
<div class="ajcxy-com the-best-website btn-group commentBtn" role="group">
<input name="submit" type="submit" id="submit" class="ajcxy-com the-best-website btn btn-sm btn-danger btn-block" tabindex="5" value="发表评论" /></div>
<?php comment_id_fields(); ?>
</form>
<?php endif; ?>
</div>
<?php endif; ?>
</div>

然后在主题的 functions.php 中加入自定义的评论列表展示样式:

//自定义评论列表模板
function simple_comment($comment, $args, $depth) {
$GLOBALS['comment'] = $comment; ?>
<li class="ajcxy-com the-best-website comment" id="li-comment-<?php comment_ID(); ?>">
<div class="ajcxy-com the-best-website media">
<div class="ajcxy-com the-best-website media-left">
<?php if (function_exists('get_avatar') && get_option('show_avatars')) { echo get_avatar($comment, 48); } ?>
</div>
<div class="ajcxy-com the-best-website media-body">
<?php printf(__('<p class="ajcxy-com the-best-website author_name">%s</p>'), get_comment_author_link()); ?>
<?php if ($comment->comment_approved == '0') : ?>
<em>评论等待审核...</em><br />
<?php endif; ?>
<?php comment_text(); ?>
</div>
</div>
<div class="ajcxy-com the-best-website comment-metadata">
<span class="ajcxy-com the-best-website comment-pub-time">
<?php echo get_comment_time('Y-m-d H:i'); ?>
</span>
<span class="ajcxy-com the-best-website comment-btn-reply">
<i class="ajcxy-com the-best-website fa fa-reply"></i> <?php comment_reply_link(array_merge( $args, array('reply_text' => '回复','depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
</span>
</div>
<?php
}
?>

最后添加下 css

/*
comments
*/
#comments{
padding-bottom:30px;
line-height:1;
}
.comments-title{
position:relative;
padding-bottom:10px;
font-size:18px;
color:#666;
border-bottom:1px solid #f1f1f1;
}
h3.comments-title:after{
position:absolute;
content:'';
top:29px;
left:0;
width:135px;
height:1px;
background:#fa2a2a;/*main-color*/
}
.commentCount{
color:#fa2a2a;/*main-color*/
}
.commentlist{
list-style: none;
margin:10px 0 0;
padding:0;
}
.commentlist ol{
list-style: none;
}
.comment{
padding:10px 0 5px;
}
.comment .media-left{
display: table-cell;
vertical-align: top;
padding-right: 10px;
}
.comment .media-left img{
border-radius:50%;
}
.comment .media-body{
display: table-cell;
vertical-align: top;
}
.comment .media-body .author_name{
margin-bottom:5px;
font-size:14px;
color:#777;
}
.comment .media-body p{
font-size:14px;
line-height:1.5em;
color:#777;
}
.comment .media-body p a{
color:#000;
}
.comment .comment-metadata{
margin-left:58px;
padding:5px 0;
}
.comment .comment-metadata span{
margin-right:15px;
font-size:13px;
}
.comment .comment-metadata span{
font-size:12px;
color:#999;
}
.comment .comment-metadata span.comment-btn-reply a:hover{
color:#666;
}
.comment .comment-metadata span.comment-btn-reply i{
color:#d1d1d1;
}
.comment .comment-metadata span.comment-btn-reply a{
color:#999;
}
.commentlist > .comment{
border-bottom:1px dotted #d9d9d9;
}
.commentlist .children{
padding-left:58px;
}
.commentlist .children > .comment{
border-top:1px dotted #d9d9d9;
}
#reply-title{
font-size:14px;
color:#666;
border-bottom:0;
color:#999;
}
#reply-title a:first-child,.warning-text a:first-child{
display:inline-block;
margin:0 2px;
padding:2px 5px;
background:#fa2a2a;/*main-color*/
color:#fff;
font-size:14px;
}
#reply-title #cancel-comment-reply-link{
background:#fff;
color:#999;
}
#reply-title #cancel-comment-reply-link:hover{
text-decoration:underline;
}
.warning-text{
color:#999;
}
.link-logout{
color:#999;
}
.comment-navigation{
width:100%;
margin:0 auto;
padding:15px 0;
text-align: center;
}
.comment-navigation .page-numbers{
display:inline-block;
padding:9px 16px;
color:#999;
background:#f1f1f1;
}
.comment-navigation .page-numbers:hover{
background:#e8e8e8;
color:#666;
}
.comment-navigation .current,
.comment-navigation .current:hover{
background:#fa2a2a;/*main-color*/
color:#fff;
}
textarea#comment{
display: block;
width: 100%;
margin-bottom:10px;
background:#f5f5f5;
color:#777;
border:0;
box-shadow:none;
height:90px;
padding:10px;
resize:none;
border-radius:5px;
transition:background .3s;
}
textarea#comment:focus{
background:#e9e9e9;
}
.commentform-info{
float:left;
}
.commentform-info input{
margin-right:10px;
width:230px;
border:0;
border-radius:0;
box-shadow:none;
background:#f5f5f5;
font-weight:normal;
transition:background .5s;
}
.commentform-info input:focus{
box-shadow:none;
color:#fff!important;
background:#fa2a2a/*main-color*/
}
.commentform-info input:focus:-moz-input-placeholder {
color: #fff;
}
.commentform-info input:focus::-moz-input-placeholder {
color: #fff;
}
.commentform-info input:focus::-webkit-input-placeholder {
color: #fff;
}
.commentform-info input:focus:-ms-input-placeholder {
color: #fff;
}
.commentBtn .btn{
padding:8px 0;
width:140px!important;
text-align: center;
background:#fa2a2a;/*main-color*/
border:0;
}
.commentBtn .btn:hover{
opacity:0.8;
}
input#submit{color: #fff;}

给TA打赏
共{{data.count}}人
人已打赏
网络教程

微信技巧微信上线新功能:拍一拍

2022-8-14 11:35:21

网络教程

移动端启用不同主题的实现办法

2022-7-15 2:00:19

18 条回复 A文章作者 M管理员
  1. pos机的那些事

    pos机的那些事函数模板里面条用在页面多出来一个?>是为什么啊4年前 (2018-09-13)陌小雨网站管理员@pos机的那些事 可能是两个原因的结合吧 一你本身的文件是以?>结尾的,第二这个函数没有放到文件的最后面4年前 (2018-10-23)

  2. 小牧

    小牧感谢博主,帮大忙了4年前 (2018-10-29)

  3. 国

    国头像是怎么换的?4年前 (2018-03-12)陌小雨网站管理员@国 头像默认的是调用的avatar头像4年前 (2018-03-15)

  4. 程序圆

    陌小雨网站管理员@国 头像默认的是调用的avatar头像4年前 (2018-03-15)

  5. 宇轩

    宇轩可以分享下,本站的评论的源码吗?4年前 (2018-02-10)陌小雨网站管理员@宇轩 根据文章中分享的办法,可以做出任何你想做的效果哦4年前 (2018-03-02)

  6. 程序圆

    陌小雨网站管理员@宇轩 根据文章中分享的办法,可以做出任何你想做的效果哦4年前 (2018-03-02)

  7. 狂放

    狂放那个有滑稽嵌套吗5年前 (2018-01-01)陌小雨网站管理员@狂放 滑稽嵌套?5年前 (2018-01-04)

  8. 程序圆

    陌小雨网站管理员@狂放 滑稽嵌套?5年前 (2018-01-04)

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索