薇晓朵技术论坛 版块列表 入门与支持 功能改进 WordPress 代码段,可以显示当前文章作者的全部评论和回复统计 通过简码调用

正在查看 1 帖子:1-1 (共 1 个帖子)
  • 作者
    帖子
  • 诗语诗语
    管理员
      @feibisi
      文章数量:5689

      还是为博客互动活跃度考虑,显示效果如下

      function get_author_comments_count( $atts ) {
          global $wpdb;
          
          $author_id = get_post_field( 'post_author' ); // 获取当前文章作者 ID
          
          $comments_count = $wpdb->get_var(
              $wpdb->prepare(
                  "SELECT COUNT(*) FROM {$wpdb->comments} WHERE user_id = %d AND comment_approved = %d",
                  $author_id,
                  1
              )
          );
          
          $replies_count = $wpdb->get_var(
              $wpdb->prepare(
                  "SELECT COUNT(*) FROM {$wpdb->comments} WHERE user_id = %d AND comment_approved = %d AND comment_parent != %d",
                  $author_id,
                  1,
                  0
              )
          );
          
          $output = "作者共有 {$comments_count} 条评论和 {$replies_count} 条回复";
          
          return $output;
      }
      add_shortcode( 'author_comments_count', 'get_author_comments_count' );

      在文章中使用 [author_comments_count] 简码即可。

    正在查看 1 帖子:1-1 (共 1 个帖子)

    抱歉,回复话题必需登录。