0

フロントエンドのWordpressで使用する次のphpを使用して、ブログの合計コメント数を表示しています。たとえば、この効果のために何かを表示するためにレンダリングするとします: 1,265,788

<?php
$comments_count = wp_count_comments();
echo number_format($comments_count->total_comments);
?>

私がやりたいのは、数値をより適切にフォーマットすることです。つまり、1,265,788 のコメントがあると表示される代わりに、1,265M のコメントがあると表示されます。

別の投稿で推奨されているように、次のコードを試しましたが、どちらも機能しません。それは完全な数をエコーし​​ます。

<?php
$comments_count = wp_count_comments();
if ($comments_count->total_comments < 1000000) {
    // Anything less than a million
    $n_format = number_format($comments_count->total_comments);
    echo $n_format;
} else if ($comments_count->total_comments < 1000000000) {
    // Anything less than a billion
    $n_format = number_format($comments_count->total_comments / 1000000, 3) . 'M';
    echo $n_format;
} else {
    // At least a billion
    $n_format = number_format($comments_count->total_comments / 1000000000, 3) . 'B';
    echo $n_format;
}
?>

いいえ、これは重複した質問ではありません。以前の回答は、私にはまったく役に立ちません。私は答えが言ったように試してみましたが、元のトップコードが私に与えたのと同じように、私が得た出力は完全な数です。

これを達成する方法を知っている人は誰でも、サンプルを見せてください。

ありがとうございました!

4

1 に答える 1