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

首頁 > 開發 > PHP > 正文

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

2024-05-04 23:40:56
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了給WordPress中的留言加上樓層號的PHP代碼實例,這里只針對主評論而不針對層疊式的樓中樓里的評論,需要的朋友可以參考下
 

最近突然發現博客的評論樓層有點問題,之前一直設置的是“在每個頁面頂部顯示新的評論”,也就是所謂的倒序顯示評論,但是主題只支持順序的評論樓層好,于是樓層和樓層號之間對不上。搜了一下在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 = '<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">'; } }

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

<?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 = '<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 }

樣式就自己添加吧~~



注:相關教程知識閱讀請移步到PHP教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 阿鲁科尔沁旗| 镇远县| 双峰县| 蒙自县| 绍兴县| 枝江市| 台南县| 遵义县| 贵溪市| 黄平县| 长海县| 上高县| 株洲县| 渝北区| 青阳县| 醴陵市| 安达市| 聊城市| 渑池县| 蒙山县| 正阳县| 陈巴尔虎旗| 苏州市| 墨竹工卡县| 张家界市| 格尔木市| 进贤县| 古蔺县| 承德县| 青川县| 若尔盖县| 永胜县| 平顶山市| 礼泉县| 玛沁县| 蒙山县| 盈江县| 屏边| 沅江市| 偏关县| 威远县|