首页 > CMS建站 > Wordpress > 正文

给WordPress中的留言加上楼层号的PHP代码实例

2021-12-03 12:15:00
字体:
来源:转载
供稿:网友

最近突然发现博客的评论楼层有点问题,之前一直设置的是“在每个页面顶部显示新的评论”,也就是所谓的倒序显示评论,但是主题只支持顺序的评论楼层好,于是楼层和楼层号之间对不上。搜了一下在zww.me发现有实现的代码,但是放到博客之后无法正常工作,比如限制分页显示为25条的时候,文章只有一条评论时也显示的25楼。折腾了一下搞定了,做个记录,也供大家参考。

在主题文件 functions.php中找到$GLOBALS['comment'] = $comment;在后面加上下面的代码:

  1. /* 主评论计数器 */ 
  2.  global $commentcount,$wpdb$post
  3.  if(!$commentcount) { //初始化楼层计数器 
  4.   if ( get_option('comment_order') === 'desc' ) { //倒序 
  5.   $comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_post_ID = $post->ID AND comment_type = '' AND comment_approved = '1' AND !comment_parent"); 
  6.   $cnt = count($comments);//获取主评论总数量 
  7.   $page = get_query_var('cpage');//获取当前评论列表页码 
  8.   $cpp=get_option('comments_per_page');//获取每页评论显示数量 
  9.   if (ceil($cnt / $cpp) == 1 || ($page > 1 && $page == ceil($cnt / $cpp))) { 
  10.    $commentcount = $cnt + 1;//如果评论只有1页或者是最后一页,初始值为主评论总数 
  11.   } else { 
  12.    $commentcount = $cpp * $page + 1; 
  13.   } 
  14.   }else//顺序 
  15.   $page = get_query_var('cpage')-1; 
  16.   $cpp=get_option('comments_per_page');//获取每页评论数 
  17.   $commentcount = $cpp * $page
  18.   } 
  19.  } 
  20. /* 主评论计数器 end */ 
  21.  if ( !$parent_id = $comment->comment_parent ) { 
  22.   $commentcountText = '<div class="floor">'
  23.   if ( get_option('comment_order') === 'desc' ) { //倒序 
  24.   $commentcountText .= --$commentcount . '楼'
  25.   } else { 
  26.   switch ($commentcount) { 
  27.    case 0: 
  28.    $commentcountText .= '<span>沙发!</span>'; ++$commentcount
  29.    break
  30.    case 1: 
  31.    $commentcountText .= '<span>板凳!</span>'; ++$commentcount
  32.    break
  33.    case 2: 
  34.    $commentcountText .= '<span>地板!</span>'; ++$commentcount
  35.    break
  36.    default
  37.    $commentcountText .= ++$commentcount . '楼'
  38.    break
  39.   } 
  40.   } 
  41.   $commentcountText .= '</div">'
  42.  } 
  43.  } 

然后在合适的位置加上以下代码输出楼层号

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

修改之后的代码应该是这样的(以官方最新的 wp_list_comments() 回调函数代码为例):

  1. <?php 
  2. function mytheme_comment($comment$args$depth) { 
  3.  $GLOBALS['comment'] = $comment
  4.  /* 主评论计数器 by zwwooooo Modified Gimhoy(http://blog.gimhoy.com) */ 
  5.  global $commentcount,$wpdb$post
  6.  if(!$commentcount) { //初始化楼层计数器 
  7.   if ( get_option('comment_order') === 'desc' ) { //倒序 
  8.   $comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_post_ID = $post->ID AND comment_type = '' AND comment_approved = '1' AND !comment_parent"); 
  9.   $cnt = count($comments);//获取主评论总数量 
  10.   $page = get_query_var('cpage');//获取当前评论列表页码 
  11.   $cpp=get_option('comments_per_page');//获取每页评论显示数量 
  12.   if (ceil($cnt / $cpp) == 1 || ($page > 1 && $page == ceil($cnt / $cpp))) { 
  13.    $commentcount = $cnt + 1;//如果评论只有1页或者是最后一页,初始值为主评论总数 
  14.   } else { 
  15.    $commentcount = $cpp * $page + 1; 
  16.   } 
  17.   }else//顺序 
  18.   $page = get_query_var('cpage')-1; 
  19.   $cpp=get_option('comments_per_page');//获取每页评论数 
  20.   $commentcount = $cpp * $page
  21.   } 
  22.  } 
  23.  /* 主评论计数器 end */ 
  24.  if ( !$parent_id = $comment->comment_parent ) { 
  25.   $commentcountText = '<div class="floor">'
  26.   if ( get_option('comment_order') === 'desc' ) { //倒序 
  27.   $commentcountText .= --$commentcount . '楼'
  28.   } else { 
  29.   switch ($commentcount) { 
  30.    case 0: 
  31.    $commentcountText .= '<span>沙发!</span>'; ++$commentcount
  32.    break
  33.    case 1: 
  34.    $commentcountText .= '<span>板凳!</span>'; ++$commentcount
  35.    break
  36.    case 2: 
  37.    $commentcountText .= '<span>地板!</span>'; ++$commentcount
  38.    break
  39.    default
  40.    $commentcountText .= ++$commentcount . '楼'
  41.    break
  42.   } 
  43.   } 
  44.   $commentcountText .= '</div">'
  45.  } 
  46.  } 
  47.  
  48.  extract($args, EXTR_SKIP); 
  49.  
  50.  if ( 'div' == $args['style'] ) { 
  51.  $tag = 'div'
  52.  $add_below = 'comment'
  53.  } else { 
  54.  $tag = 'li'
  55.  $add_below = 'div-comment'
  56.  } 
  57. ?> 
  58.  <<?php echo $tag ?> <?php comment_class(emptyempty$args['has_children'] ) ? '' : 'parent') ?> id="comment-<?php comment_ID() ?>"
  59.  <?php if ( 'div' != $args['style'] ) : ?> 
  60.  <div id="div-comment-<?php comment_ID() ?>" class="comment-body"
  61.  <?php endif; ?> 
  62.  <div class="comment-author vcard"
  63.  <?php if ($args['avatar_size'] != 0) echo get_avatar( $comment$args['avatar_size'] ); ?> 
  64.  <?php printf(__('<cite class="fn">%s</cite> <span class="says">says:</span>'), get_comment_author_link()) ?> 
  65.  </div> 
  66. <?php if ($comment->comment_approved == '0') : ?> 
  67.  <em class="comment-awaiting-moderation"><?php _e('Your comment is awaiting moderation.') ?></em> 
  68.  <br /> 
  69. <?php endif; ?> 
  70.  
  71.  <div class="comment-meta commentmetadata"><a href="<?php echo htmlspecialchars( get_comment_link( $comment->comment_ID ) ) ?>"
  72.  <?php 
  73.   /* translators: 1: date, 2: time */ 
  74.   printf( __('%1$s at %2$s'), get_comment_date(), get_comment_time()) ?></a><?php edit_comment_link(__('(Edit)'),' ','' ); 
  75.  ?> 
  76.  </div> 
  77.  
  78.  <?php comment_text() ?> 
  79.  
  80.  <div class="reply"
  81.  <?php comment_reply_link(array_merge$argsarray('add_below' => $add_below'depth' => $depth'max_depth' => $args['max_depth']))) ?> 
  82.  </div> 
  83.  
  84.  <?php echo $commentcountText//主评论楼层号 - by zwwooooo ?> 
  85.  
  86.  <?php if ( 'div' != $args['style'] ) : ?> 
  87.  </div> 
  88.  <?php endif; ?> 
  89. <?php 
  90.  } 

样式就自己添加吧~~

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表