时间:2021-07-01 10:21:17 帮助过:3人阅读
在主题文件 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 .= '沙发!'; ++$commentcount; break; case 1: $commentcountText .= '板凳!'; ++$commentcount; break; case 2: $commentcountText .= '地板!'; ++$commentcount; break; default: $commentcountText .= ++$commentcount . '楼'; break; } } $commentcountText .= '
样式就自己添加吧~~