douhulao7642 2017-06-07 14:10
浏览 69
已采纳

如何在wordpress中获得个人评论的回复计数

I use traded comments so each comments receive multiple replies in it. I need to get replies count of each individual comment. I don't need the total replies count of all the comments.

I tried to use this code:

$comment_id = get_comment_ID();

$childrenCount = get_comment_meta( $comment_id, "childrenCount" );

$childrenCountNumber = count($childrenCount);

echo $childrenCountNumber

The other way round that shows total replies which not what I'm looking for.. This code shows all the replies of all the comments.`

Code in functions.php

function replies_counter($id){
global $wpdb;
$query = "SELECT COUNT(comment_post_id) AS count FROM $wpdb->comments 
WHERE `comment_approved` = 1 AND `comment_post_ID` = $id AND 
`comment_parent` = 0";
$parents = $wpdb->get_row($query);
return $parents->count;
}

Then echo out in the template page:

$parents_count = replies_counter($post->ID);
$children_count = $post->comment_count - $parents_count;

echo $children_count;

This one is all about getting total replies count of all the comments. But I need replies count of each comment individually.

It will be nice if someone help me.

  • 写回答

2条回答 默认 最新

  • douquan3294 2017-06-07 14:49
    关注

    why don't you write such a custom function to get subcomments for each individual comment by ID like this?

    function subreplies_counter($id){
      global $wpdb;
      $query = "SELECT COUNT(comment_ID) AS count FROM $wpdb->comments 
      WHERE `comment_approved` = 1 AND `comment_parent` = $id";
      $data = $wpdb->get_row($query);
      return $data->count;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C