欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

給WordPress中的留言加上樓層號(hào)的PHP代碼實(shí)例

 更新時(shí)間:2015年12月14日 15:06:27   投稿:goldensun  
這篇文章主要介紹了給WordPress中的留言加上樓層號(hào)的PHP代碼實(shí)例,這里只針對(duì)主評(píng)論而不針對(duì)層疊式的樓中樓里的評(píng)論,需要的朋友可以參考下

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

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

/* 主評(píng)論計(jì)數(shù)器 */
 global $commentcount,$wpdb, $post;
 if(!$commentcount) { //初始化樓層計(jì)數(shù)器
  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);//獲取主評(píng)論總數(shù)量
  $page = get_query_var('cpage');//獲取當(dāng)前評(píng)論列表頁碼
  $cpp=get_option('comments_per_page');//獲取每頁評(píng)論顯示數(shù)量
  if (ceil($cnt / $cpp) == 1 || ($page > 1 && $page == ceil($cnt / $cpp))) {
   $commentcount = $cnt + 1;//如果評(píng)論只有1頁或者是最后一頁,初始值為主評(píng)論總數(shù)
  } else {
   $commentcount = $cpp * $page + 1;
  }
  }else{ //順序
  $page = get_query_var('cpage')-1;
  $cpp=get_option('comments_per_page');//獲取每頁評(píng)論數(shù)
  $commentcount = $cpp * $page;
  }
 }
/* 主評(píng)論計(jì)數(shù)器 end */
 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>沙發(fā)!</span>'; ++$commentcount;
   break;
   case 1:
   $commentcountText .= '<span>板凳!</span>'; ++$commentcount;
   break;
   case 2:
   $commentcountText .= '<span>地板!</span>'; ++$commentcount;
   break;
   default:
   $commentcountText .= ++$commentcount . '樓';
   break;
  }
  }
  $commentcountText .= '</div">';
 }
 }

然后在合適的位置加上以下代碼輸出樓層號(hào)

<?php echo $commentcountText; //主評(píng)論樓層號(hào) - by zwwooooo ?>

修改之后的代碼應(yīng)該是這樣的(以官方最新的 wp_list_comments() 回調(diào)函數(shù)代碼為例):

<?php
function mytheme_comment($comment, $args, $depth) {
 $GLOBALS['comment'] = $comment;
 /* 主評(píng)論計(jì)數(shù)器 by zwwooooo Modified Gimhoy(http://blog.gimhoy.com) */
 global $commentcount,$wpdb, $post;
 if(!$commentcount) { //初始化樓層計(jì)數(shù)器
  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);//獲取主評(píng)論總數(shù)量
  $page = get_query_var('cpage');//獲取當(dāng)前評(píng)論列表頁碼
  $cpp=get_option('comments_per_page');//獲取每頁評(píng)論顯示數(shù)量
  if (ceil($cnt / $cpp) == 1 || ($page > 1 && $page == ceil($cnt / $cpp))) {
   $commentcount = $cnt + 1;//如果評(píng)論只有1頁或者是最后一頁,初始值為主評(píng)論總數(shù)
  } else {
   $commentcount = $cpp * $page + 1;
  }
  }else{ //順序
  $page = get_query_var('cpage')-1;
  $cpp=get_option('comments_per_page');//獲取每頁評(píng)論數(shù)
  $commentcount = $cpp * $page;
  }
 }
 /* 主評(píng)論計(jì)數(shù)器 end */
 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>沙發(fā)!</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; //主評(píng)論樓層號(hào) - by zwwooooo ?>

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

樣式就自己添加吧~~

相關(guān)文章

  • laravel 數(shù)據(jù)遷移與 Eloquent ORM的實(shí)現(xiàn)方法

    laravel 數(shù)據(jù)遷移與 Eloquent ORM的實(shí)現(xiàn)方法

    laravel 提供了很實(shí)用的 Eloquent ORM 模型類,簡(jiǎn)單、直觀的與數(shù)據(jù)庫(kù)進(jìn)行交互。同時(shí)使用數(shù)據(jù)遷移管理數(shù)據(jù)庫(kù),可以與團(tuán)隊(duì)進(jìn)行共享以及編輯,本文詳細(xì)的介紹了laravel 數(shù)據(jù)遷移與 Eloquent ORM的實(shí)現(xiàn)方法,感興趣的可以了解一下
    2019-04-04
  • laravel框架模型中非靜態(tài)方法也能靜態(tài)調(diào)用的原理分析

    laravel框架模型中非靜態(tài)方法也能靜態(tài)調(diào)用的原理分析

    這篇文章主要介紹了laravel框架模型中非靜態(tài)方法也能靜態(tài)調(diào)用的原理,結(jié)合實(shí)例形式分析了laravel模型基類中使用魔術(shù)方法實(shí)現(xiàn)非靜態(tài)方法進(jìn)行靜態(tài)調(diào)用的相關(guān)原理,需要的朋友可以參考下
    2019-11-11
  • Yii Framework框架獲取分類下面的所有子類方法

    Yii Framework框架獲取分類下面的所有子類方法

    Yii是一個(gè)基于組件、用于開發(fā)大型 Web 應(yīng)用的 高性能 PHP 框架。Yii 幾乎擁有了 所有的特性 ,包括 MVC、DAO/ActiveRecord、I18N/L10N、caching、基于 JQuery 的 AJAX 支持、用戶認(rèn)證和基于角色的訪問控制、腳手架、輸入驗(yàn)證、部件、事件、主題化以及 Web 服務(wù)等等。
    2014-06-06
  • php微信公眾號(hào)js-sdk開發(fā)應(yīng)用

    php微信公眾號(hào)js-sdk開發(fā)應(yīng)用

    這篇文章主要為大家詳細(xì)介紹了php微信公眾號(hào)js-sdk開發(fā)應(yīng)用的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • 老生常談PHP中的數(shù)據(jù)結(jié)構(gòu):DS擴(kuò)展

    老生常談PHP中的數(shù)據(jù)結(jié)構(gòu):DS擴(kuò)展

    下面小編就為大家?guī)硪黄仙U凱HP中的數(shù)據(jù)結(jié)構(gòu):DS擴(kuò)展。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-07-07
  • 如何重寫Laravel異常處理類詳解

    如何重寫Laravel異常處理類詳解

    這篇文章主要給大家介紹了關(guān)于如何重寫Laravel異常處理類的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • PHP實(shí)現(xiàn)用戶認(rèn)證及管理完全源碼

    PHP實(shí)現(xiàn)用戶認(rèn)證及管理完全源碼

    PHP實(shí)現(xiàn)用戶認(rèn)證及管理完全源碼...
    2007-03-03
  • laravel-admin 在列表頁添加自定義按鈕的例子

    laravel-admin 在列表頁添加自定義按鈕的例子

    今天小編就為大家分享一篇laravel-admin 在列表頁添加自定義按鈕的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-09-09
  • destoon實(shí)現(xiàn)調(diào)用自增數(shù)字從1開始的方法

    destoon實(shí)現(xiàn)調(diào)用自增數(shù)字從1開始的方法

    這篇文章主要介紹了destoon實(shí)現(xiàn)調(diào)用自增數(shù)字從1開始的方法,很有實(shí)用價(jià)值的一個(gè)技巧,需要的朋友可以參考下
    2014-08-08
  • laravel5環(huán)境隱藏index.php后綴(apache)的方法

    laravel5環(huán)境隱藏index.php后綴(apache)的方法

    今天小編就為大家分享一篇laravel5環(huán)境隱藏index.php后綴(apache)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-10-10

最新評(píng)論