支持评论分页的wordpress评论楼层实现方法。如果你的wordpress开启了评论镶嵌,并且评论比较多情况下就会显的非常乱,而给wordpress增加评论楼层显示,就可以在一定程度上解决评论显示过乱的问题。
Young Free使用的是知更鸟的HotNewsPro2.7主题,这款主题的默认评论楼层方式是正序的。虽然能够将最新评论置于最高层,但是最高楼层却始终是1楼,不能像大厦一样,最底层是1楼。在网上找了很多代码,几经折腾,终于将评论楼层采用倒序方式显示。
实现支持评论分页的楼层显示方法的前提条件:
1. 要在 WordPress 后台开启嵌套评论,2. 所用主题支持嵌套(目前的主题基本都支持吧)
把下面的代码扔function.php里面,不过要注意的是其中的mytheme_comment 回调函数,要与comments.php的相应函数相对应。
开始添加楼层计数参数 $commentcount 等代码,具体看代码中的注释,
function mytheme_comment($comment, $args, $depth){
$GLOBALS[‘comment’] = $comment;//主评论计数器初始化 begin – by zwwooooo
global $commentcount;
if(!$commentcount) { //初始化楼层计数器
$page = get_query_var(‘cpage’)-1;
$cpp=get_option(‘comments_per_page’);//获取每页评论数
$commentcount = $cpp * $page;
}
//主评论计数器初始化 end – by zwwooooo?>
<li <?php comment_class(); ?> id=”li-comment-<?php comment_ID() ?>”>
<div id=”comment-<?php comment_ID(); ?>”>
<div>
<?php echo get_avatar($comment,$size=’48’,$default='<path_to_url>’ ); ?>
<?php printf(__(‘<cite>%s</cite> <span>says:</span>’), get_comment_author_link()) ?>
</div>
<?php if ($comment->comment_approved == ‘0’) : ?>
<em><?php _e(‘Your comment is awaiting moderation.’) ?></em>
<br />
<?php endif; ?>
<div><a href=”<?php echo htmlspecialchars( get_comment_link( $comment->comment_ID ) ) ?>”><?php printf(__(‘%1$s at %2$s’), get_comment_date(), get_comment_time()) ?></a><?php edit_comment_link(__(‘(Edit)’),’ ‘,”) ?></div>
<?php comment_text() ?>
<div>
<?php comment_reply_link(array_merge( $args, array(‘depth’ => $depth, ‘max_depth’ => $args[‘max_depth’]))) ?>
</div>
<div><!– 主评论楼层号 by zwwooooo –>
<?php if(!$parent_id = $comment->comment_parent) {printf(‘#%1$s’, ++$commentcount);} ?><!– 当前页每个主评论自动+1 –>
</div>
</div>
<?php }
需要用到的css样式
ol.commentlist li div.floor{position:absolute;top:0;right:0;}
上面的代码可以实现正序评论楼层显示,下面的代码则可以实现倒序评论楼层显示,前提条件除了上面的两点外还得需要设置WP嵌套评论排序为“由新到旧”
function mytheme_comment($comment, $args, $depth){
$GLOBALS[‘comment’] = $comment;/* 主评论计数器 by zwwooooo */
global $commentcount,$wpdb, $post;
if(!$commentcount) { //初始化楼层计数器
$comments = $wpdb->get_results(“SELECT * FROM $wpdb->comments WHERE comment_post_ID = $post->ID AND comment_type = ” AND comment_approved = ‘1’ AND !comment_parent”);
$cnt = count($comments);//获取主评论总数量
$page = get_query_var(‘cpage’);//获取当前评论列表页码
$cpp=get_option(‘comments_per_page’);//获取每页评论显示数量
if (ceil($cnt / $cpp) == 1 || ($page > 1 && $page == ceil($cnt / $cpp))) {
$commentcount = $cnt + 1;//如果评论只有1页或者是最后一页,初始值为主评论总数
} else {
$commentcount = $cpp * $page + 1;
}
}
/* 主评论计数器 end */?>
<li <?php comment_class(); ?> id=”li-comment-<?php comment_ID() ?>”>
<div id=”comment-<?php comment_ID(); ?>”>
<div>
<?php echo get_avatar($comment,$size=’48’,$default='<path_to_url>’ ); ?>
<?php printf(__(‘<cite>%s</cite> <span>says:</span>’), get_comment_author_link()) ?>
</div>
<?php if ($comment->comment_approved == ‘0’) : ?>
<em><?php _e(‘Your comment is awaiting moderation.’) ?></em>
<br />
<?php endif; ?>
<div><a href=”<?php echo htmlspecialchars( get_comment_link( $comment->comment_ID ) ) ?>”><?php printf(__(‘%1$s at %2$s’), get_comment_date(), get_comment_time()) ?></a><?php edit_comment_link(__(‘(Edit)’),’ ‘,”) ?></div>
<?php comment_text() ?>
<div>
<?php comment_reply_link(array_merge( $args, array(‘depth’ => $depth, ‘max_depth’ => $args[‘max_depth’]))) ?>
</div>
<div><!– 主评论楼层号 by zwwooooo –>
<?php if(!$parent_id = $comment->comment_parent) {printf(‘#%1$s’, –$commentcount);} ?><!– 当前页每个主评论自动-1 –>
</div>
</div>
<?php }
显示楼层号的 .floor 参考下面的 css
ol.commentlist li div.floor{position:absolute;top:0;right:0;}
代码来自zwwooooo
如果你的博客主题和YF一样,使用的是知更鸟的HotNewsPro,那么你可以直接复制我已经修改好了的代码,覆盖主题自带的代码即可。
function mytheme_comment($comment, $args, $depth) {
$GLOBALS[‘comment’] = $comment;
global $commentcount,$wpdb, $post;
if(!$commentcount) {
$comments = $wpdb->get_results(“SELECT * FROM $wpdb->comments WHERE comment_post_ID = $post->ID AND comment_type = ” AND comment_approved = ‘1’ AND !comment_parent”);
$cnt = count($comments);
$page = get_query_var(‘cpage’);
$cpp=get_option(‘comments_per_page’);
if (ceil($cnt / $cpp) == 1 || ($page > 1 && $page == ceil($cnt / $cpp))) {
$commentcount = $cnt + 1;
} else {
$commentcount = $cpp * $page + 1;
}
}
?>
<li <?php comment_class(); ?> id=”comment-<?php comment_ID() ?>”>
<div id=”div-comment-<?php comment_ID() ?>”>
<?php $add_below = ‘div-comment’; ?>
<div>
<div style=”display:none;” id=”comment-<?php comment_ID(); ?>”></div>
<span id=”avatar”>
<?php if (is_admin_comment($comment->comment_ID)){ ?>
<?php echo get_avatar( $comment, 32 ); ?>
<?php } else { echo get_avatar( $comment, 48 ); } ?>
</span>
<span >
<strong><?php comment_author_link() ?></strong> :
<span>
<?php comment_date(‘Y年m月d日’) ?><?php comment_time(‘H:i:s’) ?><?php edit_comment_link(‘编辑’,’+’,”); ?>
<span><?php if(!$parent_id = $comment->comment_parent) {printf(‘ %1$s楼’, –$commentcount);} ?></span>
<span><?php comment_reply_link(array_merge( $args, array(‘reply_text’ => ‘ @回复’, ‘add_below’ =>$add_below, ‘depth’ => $depth, ‘max_depth’ => $args[‘max_depth’]))); ?></span>
</span>
<span><?php comment_reply_link(array_merge( $args, array(‘reply_text’ => ‘回复’, ‘add_below’ =>$add_below, ‘depth’ => $depth, ‘max_depth’ => $args[‘max_depth’]))); ?></span>
</span >
</div>
<?php if ( $comment->comment_approved == ‘0’ ) : ?>
您的评论正在等待审核中…
<br/>
<?php endif; ?>
<?php comment_text() ?>
<div></div>
</div>
<?php
}function mytheme_end_comment() {
echo ‘</li>’;
}
自行修改切记一定要将++$commentcount修改为–$commentcount否则会出错。