WordPress文章主评论添加楼层号的方法代码

yibin 2016-06-01 WordPress 520

以前帮别人仿了几个模板都用到评论楼层号的效果,使用的是ZWWoOoOo分享的支持顺序和倒序只在WP主评论楼层号的代码,有了功能代码,剩下在模板实现的操作方法很简单,只需要在相应位置添加作者提供的代码,然后美化样式就可以了,对于有模板制作经验的博主来说相当容易!下面是转自ZWWoOoOo的代码。

前提条件:

  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() 回调函数作为例子来说明。

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

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    
    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
    	}
  • 然后在 $GLOBALS[‘comment’] = $comment; 这句下面添加楼层号处理函数,具体看下面修改好的的代码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    
    <?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 = ( !empty($in_comment_loop) ) ? get_query_var('cpage') : get_page_of_comment( $comment->comment_ID, $args ); //获取当前评论列表页码
    			$cpp = get_option('comments_per_page'); //获取每页评论显示数量
    			if ( get_option('comment_order') === 'desc' ) { //倒序
    				$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); //获取主评论总数量
    				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
    	}

注:底部显示位置可以自己根据自己所用主题选择

代码摘自:ZWWoOoOo

扫码添加微信

13013082126 扫描微信 建站咨询 优化咨询