給WordPress中的留言加上樓層號的PHP代碼執行個體_php執行個體

來源:互聯網
上載者:User
最近突然發現部落格的評論樓層有點問題,之前一直設定的是“在每個頁面頂部顯示新的評論”,也就是所謂的倒序顯示評論,但是主題只支援順序的評論樓層好,於是樓層和樓層號之間對不上。搜了一下在zww.me發現有實現的代碼,但是放到部落格之後無法正常工作,比如限制分頁顯示為25條的時候,文章只有一條評論時也顯示的25樓。折騰了一下搞定了,做個記錄,也供大家參考。

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

/* 主評論計數器 */ global $commentcount,$wpdb, $post; if(!$commentcount) { //初始化樓層計數器  if ( get_option('comment_order') === 'desc' ) { //倒序  $comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_post_ID = $post->ID AND comment_type = '' AND comment_approved = '1' AND !comment_parent");  $cnt = count($comments);//擷取主評論總數量  $page = get_query_var('cpage');//擷取當前評論列表頁碼  $cpp=get_option('comments_per_page');//擷取每頁評論顯示數量  if (ceil($cnt / $cpp) == 1 || ($page > 1 && $page == ceil($cnt / $cpp))) {   $commentcount = $cnt + 1;//如果評論只有1頁或者是最後一頁,初始值為主評論總數  } else {   $commentcount = $cpp * $page + 1;  }  }else{ //順序  $page = get_query_var('cpage')-1;  $cpp=get_option('comments_per_page');//擷取每頁評論數  $commentcount = $cpp * $page;  } }/* 主評論計數器 end */ if ( !$parent_id = $comment->comment_parent ) {  $commentcountText = '';  if ( get_option('comment_order') === 'desc' ) { //倒序  $commentcountText .= --$commentcount . '樓';  } else {  switch ($commentcount) {   case 0:   $commentcountText .= '沙發!'; ++$commentcount;   break;   case 1:   $commentcountText .= '板凳!'; ++$commentcount;   break;   case 2:   $commentcountText .= '地板!'; ++$commentcount;   break;   default:   $commentcountText .= ++$commentcount . '樓';   break;  }  }  $commentcountText .= ''; } }

然後在合適的位置加上以下代碼輸出樓層號

<?php echo $commentcountText; //主評論樓層號 - by zwwooooo ?>

修改之後的代碼應該是這樣的(以官方最新的 wp_list_comments() 回呼函數代碼為例):

<?phpfunction mytheme_comment($comment, $args, $depth) { $GLOBALS['comment'] = $comment; /* 主評論計數器 by zwwooooo Modified Gimhoy(http://blog.gimhoy.com) */ global $commentcount,$wpdb, $post; if(!$commentcount) { //初始化樓層計數器  if ( get_option('comment_order') === 'desc' ) { //倒序  $comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_post_ID = $post->ID AND comment_type = '' AND comment_approved = '1' AND !comment_parent");  $cnt = count($comments);//擷取主評論總數量  $page = get_query_var('cpage');//擷取當前評論列表頁碼  $cpp=get_option('comments_per_page');//擷取每頁評論顯示數量  if (ceil($cnt / $cpp) == 1 || ($page > 1 && $page == ceil($cnt / $cpp))) {   $commentcount = $cnt + 1;//如果評論只有1頁或者是最後一頁,初始值為主評論總數  } else {   $commentcount = $cpp * $page + 1;  }  }else{ //順序  $page = get_query_var('cpage')-1;  $cpp=get_option('comments_per_page');//擷取每頁評論數  $commentcount = $cpp * $page;  } } /* 主評論計數器 end */ if ( !$parent_id = $comment->comment_parent ) {  $commentcountText = '';  if ( get_option('comment_order') === 'desc' ) { //倒序  $commentcountText .= --$commentcount . '樓';  } else {  switch ($commentcount) {   case 0:   $commentcountText .= '沙發!'; ++$commentcount;   break;   case 1:   $commentcountText .= '板凳!'; ++$commentcount;   break;   case 2:   $commentcountText .= '地板!'; ++$commentcount;   break;   default:   $commentcountText .= ++$commentcount . '樓';   break;  }  }  $commentcountText .= ''; } } 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'] ) : ?> " class="comment-body"> <?php endif; ?>  <?php if ($args['avatar_size'] != 0) echo get_avatar( $comment, $args['avatar_size'] ); ?> <?php printf(__('%s says:'), get_comment_author_link()) ?> <?php if ($comment->comment_approved == '0') : ?> <?php _e('Your comment is awaiting moderation.') ?> 
<?php endif; ?> comment_ID ) ) ?>"> <?php /* translators: 1: date, 2: time */ printf( __('%1$s at %2$s'), get_comment_date(), get_comment_time()) ?><?php edit_comment_link(__('(Edit)'),' ','' ); ?> <?php comment_text() ?> <?php comment_reply_link(array_merge( $args, array('add_below' => $add_below, 'depth' => $depth, 'max_depth' => $args['max_depth']))) ?> <?php echo $commentcountText; //主評論樓層號 - by zwwooooo ?> <?php if ( 'div' != $args['style'] ) : ?> <?php endif; ?><?php }

樣式就自己添加吧~~

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.