Add floor number to comments in WordPress

*文
Release: 2023-03-18 17:04:01
Original
1718 people have browsed it

This article mainly introduces PHP code examples for adding floor numbers to comments in WordPress. This article is only for the main comments and not for the cascading comments in the building. I hope to be helpful.

Recently, I suddenly discovered that there is a problem with the comment floor of the blog. It has been set to "display new comments at the top of each page", which is the so-called display of comments in reverse order, but the theme only supports sequential comment floors. , so the floor and floor number do not match. After searching, I found the implementation code on zww.me, but it cannot work properly after being placed on the blog. For example, when the paging display is limited to 25 items, the 25th floor is also displayed when the article has only one comment. After some fiddling around, I got it done, and I made a record for everyone’s reference.

Find $GLOBALS['comment'] = $comment; in the theme file functions.php and add the following code after it:


/* 主评论计数器 */
 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;;
 }
 }
Copy after login


Then add the following code to the appropriate position to output the floor number


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


The modified code should It’s like this (take the latest official wp_list_comments() callback function code as an example):


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 ?>

Copy after login


Related recommendations:

PHP converts the path array into a directory tree

How to make general settings in WordPress

Practical tutorial on using WordPress to develop WeChat mini programs

The above is the detailed content of Add floor number to comments in WordPress. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!