<?php
global $wpdb;
$numcomms = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_approved = '1'");
if ( $numcomms > 0 ) {
$numcomms = number_format_i18n( $numcomms ); // 本地化數字格式(如加千分位)
}
?>
在模板中輸出:
<?php echo '全站共有 ' . $numcomms . ' 條已審核評論'; ?>
WordPress 內置函數 get_comments_number() 已包含所有類型評論:
<?php
// 在循環中或傳入 $post_id
$count = get_comments_number( $post_id );
echo '本文共有 ' . $count . ' 條評論';
?>
或使用 comments_number() 直接輸出本地化字符串:
<?php comments_number( '暫無評論', '1 條評論', '% 條評論' ); ?>
<?php
$top_level_count = get_comments( array(
'post_id' => $post_id,
'parent' => 0, // 只查父評論
'status' => 'approve',
'count' => true // 直接返回數量
) );
echo '主評論數:' . $top_level_count;
?>
comment_count post meta),直接調用 get_comments_number() 極快。