还是为博客互动活跃度考虑,显示效果如下
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] 简码即可。