我写了这篇《只在WP主评论加上楼层号的方法(支持评论分页)》后,很多朋友就问我“怎样实现倒序的主评论楼层号”,那时我折腾了一下,一直找不到只统计主评论(嵌套第一层)数量的函数(以前有总评论数的函数,但不适合),所以一直没实现。
昨晚(2010.8.2)37度高温让我很难入眠,想了一下,既然没有直接的函数,那么就只能直接用SQL过滤并统计出主评论数量,今天我在本地测试一翻,ok,搞定,唯一的缺点就是增加了数据库查询,没办法,谁叫WP自身没有此函数呢?
下面是方法,喜欢倒序显示评论的朋友可以折腾一下。
一、前提
1. 主题必须支持嵌套
2.设置WP嵌套评论排序为“由新到旧”
3. 主题有使用 mytheme_comment 回调函数(http://codex.wordpress.org/Template_Tags/wp_list_comments)
二、改造回调函数
1. WP官方的默认 mytheme_comment 回调函数
function mytheme_comment($comment, $args, $depth){ $GLOBALS['comment'] = $comment; ?> <li <?php comment_class(); ?> id="li-comment-<?php comment_ID() ?>"> <div id="comment-<?php comment_ID(); ?>"> <div class="comment-author vcard"> <?php echo get_avatar($comment,$size='48',$default='<path_to_url>' ); ?> <?php printf(__('<cite class="fn">%s</cite> <span class="says">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 class="comment-meta commentmetadata"><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 class="reply"> <?php comment_reply_link(array_merge( $args, array('depth' => $depth, 'max_depth' => $args['max_depth']))) ?> </div> </div> <?php }
2. 修改后的回调函数,具体看注释
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 class="comment-author vcard"> <?php echo get_avatar($comment,$size='48',$default='<path_to_url>' ); ?> <?php printf(__('<cite class="fn">%s</cite> <span class="says">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 class="comment-meta commentmetadata"><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 class="reply"> <?php comment_reply_link(array_merge( $args, array('depth' => $depth, 'max_depth' => $args['max_depth']))) ?> </div> <div class="floor"><!-- 主评论楼层号 by zwwooooo --> <?php if(!$parent_id = $comment->comment_parent) {printf('#%1$s', --$commentcount);} ?><!-- 当前页每个主评论自动-1 --> </div> </div> <?php }
3. 显示楼层号的 .floor 参考下面的 css
ol.commentlist li div.floor{position:absolute;top:0;right:0;}
搞定收工。
声明: 除非注明,ZWWoOoOo文章均为原创,转载请以链接形式标明本文地址
本文地址: http://zww.me/archives/25230
不搞了.折腾也很累人.
@山头人 喜欢就折腾,不喜欢就偷懒
这倒序的楼层也能加强用户的互动。也算一大亮点捏
@7cbt 折腾就是为了这个
每次来都有新收获,哈哈。
@empper 一起折腾
按文章步骤来,出现了这种错误:Call to a member function get_results() on a non-object
放狗搜了好久也没解决,望指点~~
@imcx
sql语句错误?以前测试没问题的,检查一下,最近忙,没时间再测试,稍后看看吧。
《只在WP主评论加上楼层号的方法(支持评论分页)》根据这篇文章来的话,没有错误···
@自耕农
因为我觉得不需要,嵌套是随时有人回复的,况且嵌套总是比较少,所以别浪费查询数了
这个我是在后台设置的,是不是3.1新带的功能就不知道了
@无冷
跟3.1没关系
我的评论顺序也是乱七八糟的,不安装时间顺序,就拿我的这篇文章 :http://www.lyove.com/ibox/gvan-y.html 来说,看每条评论的时间就能看得出,不是按照时间顺序来排列的,你的两种方法我都试过了,顺序都是乱的...什么原因呢?
@chzng
你可能用过那个嵌套插件,也就是说你一开始不是用WP原生的嵌套
我没有用过嵌套插件啊,我的主题的comment回调函数是在functions.php里面,和你所说的官网WP回调函数完全一样,我是按照你的步骤来添加的。现在用的是你写的倒序显示楼层的代码,评论顺序按时间排列了,但楼层还是倒序的。哪里又出问题了?
@chzng
我很好奇你有没有在“WP后台->设置->讨论”那里设置评论倒序?
这是我的主题的下载地址:http://www.lyove.com/wp-content/files/Gvan-Y.zip ,如果可能的话,你下载后能帮我查看下代码哪里出错了吗?谢谢了!