溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

利用PHP怎么在WordPress中實(shí)現(xiàn)一個(gè)留言樓層號(hào)功能

發(fā)布時(shí)間:2020-12-17 14:24:25 來(lái)源:億速云 閱讀:180 作者:Leah 欄目:開(kāi)發(fā)技術(shù)

利用PHP怎么在WordPress中實(shí)現(xiàn)一個(gè)留言樓層號(hào)功能?相信很多沒(méi)有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問(wèn)題出現(xiàn)的原因和解決方法,通過(guò)這篇文章希望你能解決這個(gè)問(wèn)題。

在主題文件 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)論列表頁(yè)碼
  $cpp=get_option('comments_per_page');//獲取每頁(yè)評(píng)論顯示數(shù)量
  if (ceil($cnt / $cpp) == 1 || ($page > 1 && $page == ceil($cnt / $cpp))) {
   $commentcount = $cnt + 1;//如果評(píng)論只有1頁(yè)或者是最后一頁(yè),初始值為主評(píng)論總數(shù)
  } else {
   $commentcount = $cpp * $page + 1;
  }
  }else{ //順序
  $page = get_query_var('cpage')-1;
  $cpp=get_option('comments_per_page');//獲取每頁(yè)評(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)論列表頁(yè)碼
  $cpp=get_option('comments_per_page');//獲取每頁(yè)評(píng)論顯示數(shù)量
  if (ceil($cnt / $cpp) == 1 || ($page > 1 && $page == ceil($cnt / $cpp))) {
   $commentcount = $cnt + 1;//如果評(píng)論只有1頁(yè)或者是最后一頁(yè),初始值為主評(píng)論總數(shù)
  } else {
   $commentcount = $cpp * $page + 1;
  }
  }else{ //順序
  $page = get_query_var('cpage')-1;
  $cpp=get_option('comments_per_page');//獲取每頁(yè)評(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
 }

看完上述內(nèi)容,你們掌握利用PHP怎么在WordPress中實(shí)現(xiàn)一個(gè)留言樓層號(hào)功能的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI