给WordPress中的留言加上楼层号

*文
发布: 2023-03-18 17:04:01
原创
1667 人浏览过

本文主要介绍了给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 = &#39;<p class="floor">&#39;;
  if ( get_option(&#39;comment_order&#39;) === &#39;desc&#39; ) { //倒序
  $commentcountText .= --$commentcount . &#39;楼&#39;;
  } else {
  switch ($commentcount) {
   case 0:
   $commentcountText .= &#39;<span>沙发!</span>&#39;; ++$commentcount;
   break;
   case 1:
   $commentcountText .= &#39;<span>板凳!</span>&#39;; ++$commentcount;
   break;
   case 2:
   $commentcountText .= &#39;<span>地板!</span>&#39;; ++$commentcount;
   break;
   default:
   $commentcountText .= ++$commentcount . &#39;楼&#39;;
   break;
  }
  }
  $commentcountText .= &#39;</p">&#39;;
 }
 }
登录后复制


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


<?php echo $commentcountText; //主评论楼层号 - by zwwooooo ?>
登录后复制


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


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

'; if ( get_option('comment_order') === 'desc' ) { //倒序 $commentcountText .= --$commentcount . '楼'; } else { switch ($commentcount) { case 0: $commentcountText .= '沙发!'; ++$commentcount; break; case 1: $commentcountText .= '板凳!'; ++$commentcount; break; case 2: $commentcountText .= '地板!'; ++$commentcount; break; default: $commentcountText .= ++$commentcount . '楼'; break; } } $commentcountText .= ''; } } extract($args, EXTR_SKIP); if ( 'p' == $args['style'] ) { $tag = 'p'; $add_below = 'comment'; } else { $tag = 'li'; $add_below = 'p-comment'; } ?> < id="comment-">

%s says:'), get_comment_author_link()) ?>

comment_approved == '0') : ?>

$add_below, 'depth' => $depth, 'max_depth' => $args['max_depth']))) ?>

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

登录后复制


相关推荐:

PHP根据路径数组转成一个目录树

WordPress中如何进行常规设置

使用WordPress开发微信小程序实战教程

以上是给WordPress中的留言加上楼层号的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!