You are here: Home » All Categories » WordPress » WP Trick » 只在WP主评论加上楼层号的方法(支持评论分页)

只在WP主评论加上楼层号的方法(支持评论分页)

May 25th, 2010 14:55 | Leave a comment?(201) Go to comments

很久没折腾 WordPress 相关的伪技术了,今天突然想起自从 WordPress 2.7 支持嵌套后久久没实现的评论楼层号,如果按照老式的方法给主评论和嵌套评论都加上楼层号,我个人认为是没有什么意义的,而且感觉会乱七八糟,但是只为主评论加上楼层号,那么就不同了,因为每层楼就是一个讨论组,可以当作是一个论坛贴,嵌套评论就是参加讨论的人。

我记得4月份在《凤凰不归巢》那里看到一篇文章《WP非插件显示谁坐了沙发》里面有相关主次评论判断方法,于是今早无聊就折腾了一番,没想到真给我折腾出来了。

效果看我现在用的主题 zSnos 的评论,下面是方法

前提:

1. 当然是要在 WordPress 后台开启嵌套评论啦
2. 所用主题支持嵌套(目前的主题基本都支持吧)
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. 那么现在开始添加楼层计数参数 $commentcount 等代码,具体看代码中的注释

2010.10.28 Update:优化了查询数)

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 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. 从上面代码可以看出我给楼层号特意加上一个 div 来显示,如果你的主题评论结构够标准,那么可以参考下面的 css

ol.commentlist li div.floor{position:absolute;top:0;right:0;}

OK,搞定,有兴趣的朋友玩玩吧,这个唯一不好的就是会增加数据库查询次数,不过这应该是微不足道。

声明: 除非注明,ZWWoOoOo文章均为原创,转载请以链接形式标明本文地址
本文地址: http://zww.me/archives/25161

Filed under

WP Trick

| Tags:

,

Related Posts

Most Popular

201 Comments.

⊕Leave a comment?
  1. atatstone atatstone Google Chrome 14.0.835.163Windows 7

    我用你的代码成功为我的博客添加了楼层显示,谢谢!

    81楼
  2. 折子戏 折子戏 Google Chrome 14.0.835.202Windows 7

    哦呵呵,支持个,这个功能准备明天就折腾。。又多了个事做了!

    82楼
  3. 折子戏 折子戏 Google Chrome 13.0.782.218Windows XP

    请问Z大:我的主题好像没有调用$commentcount函数,那我应该怎么做?

    83楼
  4. suN suN Firefox 7.0.1Windows 7 x64 Edition

    看不懂 :mrgreen:

    84楼
  5. kitsusia kitsusia Google Chrome 16.0.912.63Windows 7

    博主新年好啊! 昨天折腾了下,发现如果我10条评论每页,每个页面的评论就是从10楼开始的,好奇怪,主题是 suffusion,算是比较常见的,纠结中啊。。。

    85楼
  6. kitsusia kitsusia Google Chrome 16.0.912.63Windows 7

    哦,没错,是我折腾的时候把$page 忘记减1了, :mrgreen:

    86楼
  7. kitsusia kitsusia Google Chrome 16.0.912.63Windows 7

    我用WP-PageNavi,不过好像只能给文章分页,没法给评论分页。问下楼主,你的这个评论分页用的什么插件啊?

    87楼
  8. 88楼
  9. shadowma shadowma Firefox 9.0.1Windows 7

    試試ajax評論提交後的效果,我的新評論總是1樓

    89楼
  10. airoschou airoschou Google Chrome 16.0.912.77Windows XP

    为什么我是从#49开始的,不是#-1开始!

    90楼
    • zwwooooo zwwooooo Firefox 9.0.1Windows 7 x64 Edition

      @airoschou
      检查一下吧,注意要开启嵌套,而且这篇文章所说的方法是评论以顺序排序

Leave a Reply