0

I have the following php I use in Wordpress on the frontend to display how many total comments my blog has. So say for example would render to display something to this effect: 1,265,788

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

What I would like to do is format the number better. So instead of it saying I have 1,265,788 comments, it will say I have 1,265M comments.

I tried the following code as recommended by another post, but does not work either. It echo's the full number.

<?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;
}
?>

So no, this is not a duplicate question. Previous answers are of absolute no help to me. I tried exactly like the answers said and the output I get is the full number like the original top code gives me.

Anyone have an idea how I can achieve this and can show me a sample please.

Thank you!

Musa
  • 96,336
  • 17
  • 118
  • 137
Jay
  • 73
  • 1
  • 2
  • 8
  • I think there is a good option for you in this post : http://stackoverflow.com/questions/10221694/convert-number-into-xx-xx-million-format – John Jul 07 '13 at 19:37

1 Answers1

1

Actually above code is working fine and output is 1.266M

Hard coded example:

$number = 1265788;
if ($number < 1000000) {
    // Anything less than a million
    $n_format = number_format($number);
    echo $n_format;
} else if ($number < 1000000000) {
    // Anything less than a billion
    $n_format = number_format($number / 1000000, 3) . 'M';
    echo $n_format;
} else {
    // At least a billion
    $n_format = number_format($number / 1000000000, 3) . 'B';
    echo $n_format;
}

Dynamic:

$comments_count = wp_count_comments();
$number = $comments_count->total_comments;
if ($number < 1000000) {
    // Anything less than a million
    $n_format = number_format($number);
    echo $n_format;
} else if ($number < 1000000000) {
    // Anything less than a billion
    $n_format = number_format($number / 1000000, 3) . 'M';
    echo $n_format;
} else {
    // At least a billion
    $n_format = number_format($number / 1000000000, 3) . 'B';
    echo $n_format;
}
Manoj Yadav
  • 6,560
  • 1
  • 23
  • 21
  • I tried that. But now I get 0.000M. I used total_comments) { $n_format = number_format($number); echo $n_format; } else if ($number < 1000000000) { $n_format = number_format($number / 1000000, 3) . 'M'; echo $n_format; } else { $n_format = number_format($number / 1000000000, 3) . 'B'; echo $n_format; } ?> – Jay Jul 07 '13 at 20:06
  • I try this and get 0. total_comments; if ($number < 1000000) { // Anything less than a million $n_format = number_format($number); echo $n_format; } else if ($number < 1000000000) { // Anything less than a billion $n_format = number_format($number / 1000000, 3) . 'M'; echo $n_format; } else { // At least a billion $n_format = number_format($number / 1000000000, 3) . 'B'; echo $n_format; } ?> – Jay Jul 07 '13 at 20:14
  • That means your comment count is 0 – Manoj Yadav Jul 07 '13 at 20:31
  • No my global comment count is over 1 million – Jay Jul 07 '13 at 20:49
  • Are you initializing the `$comments_count` like this `$comments_count = wp_count_comments();` before `$number = $comments_count->total_comments;`? I have updated the answer use Dynamic: example – Manoj Yadav Jul 07 '13 at 21:04
  • I tried the Dynamic example you posted and it shows the entire comment count (ie: 1,265,788 ) but still doesnt seem to want to shorten the comment count to 1,265M – Jay Jul 07 '13 at 21:10
  • Your answer works outside of the site, but inside of it it dont. I believe something is wrong in my functions.php file. Thanks anyways – Jay Jul 07 '13 at 21:26