评论楼层错位问题背景
近期发现博客评论的楼层编号出现错位。此前将评论排序方式设为“在每个页面顶部显示新的评论”(即倒序),但所用主题仅支持顺序排列的楼层号,导致两者不匹配。参考zww.me上的相关代码后,直接部署未能生效,例如在限制每页显示25条评论时,即使文章仅有一条评论,也会错误显示为25楼。经调试修正后,现整理此解决方案供参考。
核心PHP代码修改位置
打开当前主题的functions.php文件,定位到全局变量赋值语句 `$GLOBALS['comment'] = $comment;`,在其后方插入以下代码片段:
楼层计数逻辑实现
/* 主评论计数器 */
global $commentcount,$wpdb, $post;
if(!$commentcount) { //初始化楼层计数器
if ( get_option('comment_order') === 'desc' ) { //倒序
$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;
}
}else{ //顺序
$page = get_query_var('cpage')-1;
$cpp=get_option('comments_per_page');//获取每页评论数
$commentcount = $cpp * $page;
}
}
/* 主评论计数器 end */
if ( !$parent_id = $comment->comment_parent ) {
$commentcountText = '';
if ( get_option('comment_order') === 'desc' ) { //倒序
$commentcountText .= --$commentcount . '楼';
} else {
switch ($commentcount) {
case 0:
$commentcountText .= '沙发!'; ++$commentcount;
break;
case 1:
$commentcountText .= '板凳!'; ++$commentcount;
break;
case 2:
$commentcountText .= '地板!'; ++$commentcount;
break;
default:
$commentcountText .= ++$commentcount . '楼';
break;
}
}
$commentcountText .= '';
}
}
前端楼层号输出调用
随后,在模板文件中需要展示楼层信息的位置,调用相应逻辑以输出楼层号。
<?php echo $commentcountText; //主评论楼层号 - by zwwooooo ?>
完整回调函数示例
整合后的完整代码结构如下所示,此处以官方最新的 `wp_list_comments()` 回调函数为例进行说明:
<?php
function mytheme_comment($comment, $args, $depth) {
$GLOBALS['comment'] = $comment;
/* 主评论计数器 by zwwooooo Modified Gimhoy(http://blog.gimhoy.com) */
global $commentcount,$wpdb, $post;
if(!$commentcount) { //初始化楼层计数器
if ( get_option('comment_order') === 'desc' ) { //倒序
$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;
}
}else{ //顺序
$page = get_query_var('cpage')-1;
$cpp=get_option('comments_per_page');//获取每页评论数
$commentcount = $cpp * $page;
}
}
/* 主评论计数器 end */
if ( !$parent_id = $comment->comment_parent ) {
$commentcountText = '';
if ( get_option('comment_order') === 'desc' ) { //倒序
$commentcountText .= --$commentcount . '楼';
} else {
switch ($commentcount) {
case 0:
$commentcountText .= '<span>沙发!</span>'; ++$commentcount;
break;
case 1:
$commentcountText .= '<span>板凳!</span>'; ++$commentcount;
break;
case 2:
$commentcountText .= '<span>地板!</span>'; ++$commentcount;
break;
default:
$commentcountText .= ++$commentcount . '楼';
break;
}
}
$commentcountText .= '</div">';
}
}
extract($args, EXTR_SKIP);
if ( 'div' == $args['style'] ) {
$tag = 'div';
$add_below = 'comment';
} else {
$tag = 'li';
$add_below = 'div-comment';
}
?>
<?php if ( 'div' != $args['style'] ) : ?>
" class="comment-body">
<?php endif; ?>
<?php if ($args['avatar_size'] != 0) echo get_avatar( $comment, $args['avatar_size'] ); ?>
<?php printf(__('<cite class="fn">%s</cite> <span class="says">says:</span>'), get_comment_author_link()) ?>
<?php if ($comment->comment_approved == '0') : ?>
<?php _e('Your comment is awaiting moderation.') ?>
<?php endif; ?>
comment_ID ) ) ?>">
<?php
/* translators: 1: date, 2: time */
printf( __('%1$s at %2$s'), get_comment_date(), get_comment_time()) ?><?php edit_comment_link(__('(Edit)'),' ','' );
?>
<?php comment_text() ?>
<?php comment_reply_link(array_merge( $args, array('add_below' => $add_below, 'depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
<?php echo $commentcountText; //主评论楼层号 - by zwwooooo ?>
<?php if ( 'div' != $args['style'] ) : ?>
<?php endif; ?>
<?php
}
样式定制说明
具体的CSS样式可根据个人需求自行定义。