给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 = '<p 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 .= '</p">';
 }
 }

而后在合适的地位加上以下代码输入楼层号

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

修正之后的代码应该是这样的(以民间**的 wp_list_comments() 回调函数代码为例):

<?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 = '<p 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 .= '</p">';
 }
 }

 extract($args, EXTR_SKIP);

 if ( 'p' == $args['style'] ) {
 $tag = 'p';
 $add_below = 'comment';
 } else {
 $tag = 'li';
 $add_below = 'p-comment';
 }
?>
 <<?php echo $tag ?> <?php comment_class(empty( $args['has_children'] ) ? '' : 'parent') ?> id="comment-<?php comment_ID() ?>">
 <?php if ( 'p' != $args['style'] ) : ?>
 <p id="p-comment-<?php comment_ID() ?>" class="comment-body">
 <?php endif; ?>
 <p 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()) ?>
 </p>
<?php if ($comment->comment_approved == '0') : ?>
 <em class="comment-awaiting-moderation"><?php _e('Your comment is awaiting moderation.') ?></em>
 <br />
<?php endif; ?>

 <p 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)'),' ','' );
 ?>
 </p>

 <?php comment_text() ?>

 <p class="reply">
 <?php comment_reply_link(array_merge( $args, array('add_below' => $add_below, 'depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
 </p>

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

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

款式就本人增加吧~~

以上就是安达网络工作室关于《给WordPress中的留言加上楼层号的PHP代码实例》的一些看法。更多内容请查看本栏目更多内容!

本文相关话题: WordPress 留言 评论 PHP PHP编程
版权声明:本文为 安达网络工作室 转载文章,如有侵权请联系我们及时删除。
相关文章
推荐十款收费 WordPress 插件

2015必备wordpress插件列表。为了加强wordpress站点,一些优秀无效的收费wordpress 插件是必不可少的。 Wo...

wordpress优化头部 去掉版权等信息 wordpress去掉generator

wordpress在默许状况下,头部会呈现很多平常用不到的html代码,比方: 复制代码代码如下: <link rel="altern...

让我据守ZBLOG的十二大理由

要害字形容:理由 ZBLOG 装置 优化 工夫 WordPress 能够 文章 博客   让我据守ZBLOG的十二大理由:   ...

WordPress源代码中文乱码的处理办法

查看相干教程,UTF-8和UTF-8 + BOM有所区别,于是把function.php编码改为后者。成绩处理。 UFT-8 与UTF-8 B...

WordPress完成评论提交后跳转的办法

很多采纳WordPress顺序搭建的博客都相当注重与读者之间的互动,以评论为例,为了进步读者的体验品质,有的博...

wordpress评论者链接在新窗口中关上的办法

找到wp-includes/comment-template.php文件中 复制代码代码如下:if ( empty( $url ) || 'http://' == $url ...

需求提交

客服服务

亿鸽在线客服系统