Maison développement back-end tutoriel php 给WordPress中的留言加上楼层号的PHP代码实例_PHP

给WordPress中的留言加上楼层号的PHP代码实例_PHP

May 28, 2016 pm 01:13 PM
php wordpress message 评论

最近突然发现博客的评论楼层有点问题,之前一直设置的是“在每个页面顶部显示新的评论”,也就是所谓的倒序显示评论,但是主题只支持顺序的评论楼层好,于是楼层和楼层号之间对不上。搜了一下在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">';
 }
 }
Copier après la connexion

然后在合适的位置加上以下代码输出楼层号

<&#63;php echo $commentcountText; //主评论楼层号 - by zwwooooo &#63;>
Copier après la connexion

修改之后的代码应该是这样的(以官方最新的 wp_list_comments() 回调函数代码为例):

<&#63;php
function 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';
 }
&#63;>
 <<&#63;php echo $tag &#63;> <&#63;php comment_class(empty( $args['has_children'] ) &#63; '' : 'parent') &#63;> id="comment-<&#63;php comment_ID() &#63;>">
 <&#63;php if ( 'div' != $args['style'] ) : &#63;>
 <div id="div-comment-<&#63;php comment_ID() &#63;>" class="comment-body">
 <&#63;php endif; &#63;>
 <div class="comment-author vcard">
 <&#63;php if ($args['avatar_size'] != 0) echo get_avatar( $comment, $args['avatar_size'] ); &#63;>
 <&#63;php printf(__('<cite class="fn">%s</cite> <span class="says">says:</span>'), get_comment_author_link()) &#63;>
 </div>
<&#63;php if ($comment->comment_approved == '0') : &#63;>
 <em class="comment-awaiting-moderation"><&#63;php _e('Your comment is awaiting moderation.') &#63;></em>
 <br />
<&#63;php endif; &#63;>

 <div class="comment-meta commentmetadata"><a href="<&#63;php echo htmlspecialchars( get_comment_link( $comment->comment_ID ) ) &#63;>">
 <&#63;php
  /* translators: 1: date, 2: time */
  printf( __('%1$s at %2$s'), get_comment_date(), get_comment_time()) &#63;></a><&#63;php edit_comment_link(__('(Edit)'),' ','' );
 &#63;>
 </div>

 <&#63;php comment_text() &#63;>

 <div class="reply">
 <&#63;php comment_reply_link(array_merge( $args, array('add_below' => $add_below, 'depth' => $depth, 'max_depth' => $args['max_depth']))) &#63;>
 </div>

 <&#63;php echo $commentcountText; //主评论楼层号 - by zwwooooo &#63;>

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

Copier après la connexion

样式就自己添加吧~~

Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn

Article chaud

Repo: Comment relancer ses coéquipiers
3 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
Combien de temps faut-il pour battre Split Fiction?
3 Il y a quelques semaines By DDD
R.E.P.O. Crystals d'énergie expliqués et ce qu'ils font (cristal jaune)
1 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: Comment obtenir des graines géantes
3 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌

Article chaud

Repo: Comment relancer ses coéquipiers
3 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
Combien de temps faut-il pour battre Split Fiction?
3 Il y a quelques semaines By DDD
R.E.P.O. Crystals d'énergie expliqués et ce qu'ils font (cristal jaune)
1 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: Comment obtenir des graines géantes
3 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌

Tags d'article chaud

Bloc-notes++7.3.1

Bloc-notes++7.3.1

Éditeur de code facile à utiliser et gratuit

SublimeText3 version chinoise

SublimeText3 version chinoise

Version chinoise, très simple à utiliser

Envoyer Studio 13.0.1

Envoyer Studio 13.0.1

Puissant environnement de développement intégré PHP

Dreamweaver CS6

Dreamweaver CS6

Outils de développement Web visuel

SublimeText3 version Mac

SublimeText3 version Mac

Logiciel d'édition de code au niveau de Dieu (SublimeText3)

Guide d'installation et de mise à niveau de PHP 8.4 pour Ubuntu et Debian Guide d'installation et de mise à niveau de PHP 8.4 pour Ubuntu et Debian Dec 24, 2024 pm 04:42 PM

Guide d'installation et de mise à niveau de PHP 8.4 pour Ubuntu et Debian

Date et heure de CakePHP Date et heure de CakePHP Sep 10, 2024 pm 05:27 PM

Date et heure de CakePHP

Configuration du projet CakePHP Configuration du projet CakePHP Sep 10, 2024 pm 05:25 PM

Configuration du projet CakePHP

Téléchargement de fichiers CakePHP Téléchargement de fichiers CakePHP Sep 10, 2024 pm 05:27 PM

Téléchargement de fichiers CakePHP

Routage CakePHP Routage CakePHP Sep 10, 2024 pm 05:25 PM

Routage CakePHP

Discuter de CakePHP Discuter de CakePHP Sep 10, 2024 pm 05:28 PM

Discuter de CakePHP

Guide rapide CakePHP Guide rapide CakePHP Sep 10, 2024 pm 05:27 PM

Guide rapide CakePHP

Comment configurer Visual Studio Code (VS Code) pour le développement PHP Comment configurer Visual Studio Code (VS Code) pour le développement PHP Dec 20, 2024 am 11:31 AM

Comment configurer Visual Studio Code (VS Code) pour le développement PHP

See all articles