国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > CMS > Wordpress > 正文

給WordPress中的留言加上樓層號的PHP代碼實例

2024-09-07 00:48:10
字體:
來源:轉載
供稿:網(wǎng)友

最近突然發(fā)現(xiàn)博客的評論樓層有點問題,之前一直設置的是“在每個頁面頂部顯示新的評論”,也就是所謂的倒序顯示評論,但是主題只支持順序的評論樓層好,于是樓層和樓層號之間對不上。搜了一下在zww.me發(fā)現(xiàn)有實現(xiàn)的代碼,但是放到博客之后無法正常工作,比如限制分頁顯示為25條的時候,文章只有一條評論時也顯示的25樓。折騰了一下搞定了,做個記錄,也供大家參考。

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

  1. /* 主評論計數(shù)器 */ 
  2.  global $commentcount,$wpdb$post
  3.  if(!$commentcount) { //初始化樓層計數(shù)器 
  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);//獲取主評論總數(shù)量 
  7.   $page = get_query_var('cpage');//獲取當前評論列表頁碼 
  8.   $cpp=get_option('comments_per_page');//獲取每頁評論顯示數(shù)量 
  9.   if (ceil($cnt / $cpp) == 1 || ($page > 1 && $page == ceil($cnt / $cpp))) { 
  10.    $commentcount = $cnt + 1;//如果評論只有1頁或者是最后一頁,初始值為主評論總數(shù) 
  11.   } else { 
  12.    $commentcount = $cpp * $page + 1; 
  13.   } 
  14.   }else//順序 
  15.   $page = get_query_var('cpage')-1; 
  16.   $cpp=get_option('comments_per_page');//獲取每頁評論數(shù) 
  17.   $commentcount = $cpp * $page
  18.   } 
  19.  } 
  20. /* 主評論計數(shù)器 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>沙發(fā)!</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() 回調函數(shù)代碼為例):

  1. <?php 
  2. function mytheme_comment($comment$args$depth) { 
  3.  $GLOBALS['comment'] = $comment
  4.  /* 主評論計數(shù)器 by zwwooooo Modified Gimhoy(http://blog.gimhoy.com) */ 
  5.  global $commentcount,$wpdb$post
  6.  if(!$commentcount) { //初始化樓層計數(shù)器 
  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);//獲取主評論總數(shù)量 
  10.   $page = get_query_var('cpage');//獲取當前評論列表頁碼 
  11.   $cpp=get_option('comments_per_page');//獲取每頁評論顯示數(shù)量 
  12.   if (ceil($cnt / $cpp) == 1 || ($page > 1 && $page == ceil($cnt / $cpp))) { 
  13.    $commentcount = $cnt + 1;//如果評論只有1頁或者是最后一頁,初始值為主評論總數(shù) 
  14.   } else { 
  15.    $commentcount = $cpp * $page + 1; 
  16.   } 
  17.   }else//順序 
  18.   $page = get_query_var('cpage')-1; 
  19.   $cpp=get_option('comments_per_page');//獲取每頁評論數(shù) 
  20.   $commentcount = $cpp * $page
  21.   } 
  22.  } 
  23.  /* 主評論計數(shù)器 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>沙發(fā)!</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.  } 

樣式就自己添加吧~~

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 富阳市| 龙口市| 彝良县| 壶关县| 营口市| 宜阳县| 溧阳市| 宣武区| 兴文县| 唐海县| 柯坪县| 南部县| 高平市| 大丰市| 广东省| 四平市| 呼玛县| 鹤峰县| 泰宁县| 卢龙县| 儋州市| 如皋市| 鹤壁市| 汝州市| 泸溪县| 临汾市| 抚远县| 泾川县| 出国| 北安市| 正蓝旗| 故城县| 卢龙县| 资中县| 邵阳县| 车致| 奈曼旗| 南雄市| 潍坊市| 通城县| 札达县|