只在WP主评论加上楼层号的方法『支持顺序、倒序』

2015.12.18 !!!:无意中在修改主题发现,此文章方法在 WordPress 4.4 出现错乱,我找个时间修正下。

----

今天在修改付费主题 zSnos2.5 时顺便把此主题的“主评论显示楼层号”代码优化了一下,让它支持评论顺序和倒序

基础代码可以参考我以前写的 2 篇文章:《只在WP主评论加上楼层号的方法(支持评论分页)》,《倒序显示主评论楼层号(支持评论分页)

很久没折腾这些了,当作是重新整理吧,以前都是折腾到什么就写什么,木有对相同功能的代码进行整合和优化,今天就先拿这个整合一下(还是折腾到啥写啥,囧)

前提条件:

1. 需要开启嵌套评论和评论分页功能(WP后台 》设置 》讨论)
2. 主题评论调用函数 wp_list_comments() (在 comments.php 文件里)使用了回调函数。具体参考:http://codex.wordpress.org/Template_Tags/wp_list_comments
3. 此文章所说主题评论是调用所有类型评论:comment、pingback、trackback,如果只是调用 comment 部分,需要对代码稍微修改

下面以官方给出的 wp_list_comments() 回调函数作为例子来说明。

1. 官方最新的 wp_list_comments() 回调函数代码:(这段代码一般放在主题文件 functions.php)

function mytheme_comment($comment, $args, $depth) {
	$GLOBALS['comment'] = $comment;
	extract($args, EXTR_SKIP);

	if ( 'div' == $args['style'] ) {
		$tag = 'div';
		$add_below = 'comment';
	} else {
		$tag = 'li';
		$add_below = 'div-comment';
	}
?>
	<<?php echo $tag ?> <?php comment_class(empty( $args['has_children'] ) ? '' : 'parent') ?> id="comment-<?php comment_ID() ?>">
	<?php if ( 'div' != $args['style'] ) : ?>
	<div id="div-comment-<?php comment_ID() ?>" class="comment-body">
	<?php endif; ?>
	<div class="comment-author vcard">
	<?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()) ?>
	</div>
<?php if ($comment->comment_approved == '0') : ?>
	<em class="comment-awaiting-moderation"><?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
			/* translators: 1: date, 2: time */
			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('add_below' => $add_below, 'depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
	</div>
	<?php if ( 'div' != $args['style'] ) : ?>
	</div>
	<?php endif; ?>
<?php
	}

2. 然后在 $GLOBALS['comment'] = $comment; 这句下面添加楼层号处理函数,具体看下面修改好的的代码(蓝色部分

2015.07.12 补充:有朋友说 WordPress 4.x 倒序方式时获取的总评论数失效,已改为 #42  楼的方法解决
2016.04.21 修正:忘了是 WordPress 4.x 了,$in_comment_loop 无法获取正确的值,已改为 get_query_var('cpage') 来判断(感谢 #44 楼的朋友指出)。

<?php
function mytheme_comment($comment, $args, $depth) {
	$GLOBALS['comment'] = $comment;

	 //主评论计数器 by zwwooooo
	global $commentcount, $page, $wpdb;
	if ( (int) get_option('page_comments') === 1 && (int) get_option('thread_comments') === 1 ) { //开启嵌套评论和分页才启用
		if(!$commentcount) { //初始化楼层计数器
			$page = ( get_query_var('cpage') ) ? get_query_var('cpage') : get_page_of_comment( $comment->comment_ID, $args ); //获取当前评论列表页码
			$cpp = get_option('comments_per_page'); //获取每页评论显示数量
			if ( get_option('comment_order') === 'desc' ) { //倒序
				$cnt = get_comments('post_id='.$post->ID.'&status=approve&count=true'); //获取主评论总数量
				if (ceil($cnt / $cpp) == 1 || ($page > 1 && $page  == ceil($cnt / $cpp))) { //如果评论只有1页或者是最后一页,初始值为主评论总数
					$commentcount = $cnt + 1;
				} else {
					$commentcount = $cpp * $page + 1;
				}
			} else {
				$commentcount = $cpp * ($page - 1);
			}
		}
		if ( !$parent_id = $comment->comment_parent ) {
			$commentcountText = '<div class="floor">';
			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 echo $tag ?> <?php comment_class(empty( $args['has_children'] ) ? '' : 'parent') ?> id="comment-<?php comment_ID() ?>">
	<?php if ( 'div' != $args['style'] ) : ?>
	<div id="div-comment-<?php comment_ID() ?>" class="comment-body">
	<?php endif; ?>
	<div class="comment-author vcard">
	<?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()) ?>
	</div>
<?php if ($comment->comment_approved == '0') : ?>
	<em class="comment-awaiting-moderation"><?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
			/* translators: 1: date, 2: time */
			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('add_below' => $add_below, 'depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
	</div>

	<?php echo $commentcountText; //主评论楼层号 - by zwwooooo ?>

	<?php if ( 'div' != $args['style'] ) : ?>
	</div>
	<?php endif; ?>
<?php
	}

注:(底部显示位置可以自己根据自己所用主题选择,这里我加在回复按钮后面,另外楼层显示样式可以通过 floor 这个 class 定义)

就这样吧,现在折腾 WordPress 的朋友少了很多,新朋友参考一下吧。

zww
or
oooo

“只在WP主评论加上楼层号的方法『支持顺序、倒序』”有117条评论

  1. 我把if ( get_option('comment_order') === 'desc' ) { //倒序这一句调整到$commentcount = $cpp * $page + 1;
    前面,感觉这样判断好一些,要不然首页总显示“沙发 0 沙发 0 沙发 0 ...”

    1. zwwooooo says:

      @碎碎念ZWS
      嗯啊,这代码很老了,这样改进很好

    2. 碎碎念 says:

      @zwwooooo
      经典代码,经得起折腾,哈哈

  2. too says:

    HI,谁有最新的楼层倒序方法,这个好像在4.2里,老是不能正确获取总评论数。

    1. zwwooooo says:

      @too
      刚才的回复错了,应该是:

      $comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_post_ID = $post->ID AND comment_type = 'all' AND comment_approved = '1' AND !comment_parent");
      $cnt = count($comments); //获取主评论总数量

      改为

      $cnt = get_comments('post_id='.$post->ID.'&status=approve&count=true'); //获取主评论总数量

      试试,如果ok的话,烦请回复一声,谢谢。

    2. TOO says:

      @zwwooooo
      对呢。这样改,有效果。多谢了!

    3. 郑永 says:

      @TOO
      回头看看。

  3. Fanly says:

    感觉好像使用AJAX的楼层会出错

    1. zwwooooo says:

      @Fanly 这个是很老的版本了,新的wp不一定兼容

    2. Fanly says:

      @zwwooooo 最近折腾了一下,所看才找到的,不过逻辑上还是可以学习一下

    3. zwwooooo says:

      @Fanly 的确是 4.4 上错乱了,有时间修正下。

  4. 华子 says:

    博主,请更新下
    $page = ( !empty($in_comment_loop) ) ? get_query_var('cpage') : get_page_of_comment( $comment->comment_ID, $args ); //获取当前评论列表页码
    --------------------
    这句在WordPress 4.4更新评论翻页后楼层显示错误,应该更改为:
    $page = ( get_query_var('cpage') ) ? get_query_var('cpage') : get_page_of_comment( $comment->comment_ID, $args );

    1. zwwooooo says:

      @华子 谢谢指出,不过我现在博客的主题这个“!empty($in_comment_loop)”参数判断没问题,有时间我再检测下。

    2. 3.1415926 says:

      @zwwooooo ajax评论翻页代码有分享过吗,另外提个bug,发现打开这个当前页面看评论楼层号是错的,翻到第一页再翻回来才对

    3. zwwooooo says:

      @3.1415926 应该是没写过,你搜下,其他人写过。

  5. 橘子 says:

    感谢z大!
    话说在if ( !$parent_id = $comment->comment_parent ) {这个判断加个else也减commentcount可以避免因为多个子楼而让主楼数量不对的问题

    1. zwwooooo says:

      @橘子 !$parent_id = $comment->comment_parent
      这个已经判断了是否第一级了,不过以前折腾时不知道为何加了个 $parent_id 参数,其实只要 !$comment->comment_parent 就行了

    2. 橘子 says:

      @zwwooooo 恩恩对!感谢www

  6. Jotc says:

    楼层达到50层就会显示显示为0层,是什么原因?

    1. zwwooooo says:

      @Jotc 不清楚你的评论结构是如何的,无法判断。(我这正常)

  7. Mashiro says:

    为什么不直接用SQL计算楼数呢?比如这样:
    SELECT COUNT(*) FROM `wp_comments` WHERE `comment_post_ID` = 52 AND `comment_parent` = 0 AND `comment_ID` <= 32

    文章里这个函数好像太麻烦了吧

    1. zwwooooo says:

      @Mashiro 非常老的代码了——SQL不熟悉,而且二次开发通常会优先使用 WordPress 封装的函数,后面改用 get_comments 获取了

回复给 Pang ¬
取消回复

昵称 *

网址

B em del U Link Code Quote