1

I'm trying to format my post my total post count in the header of my site. I have the number in there but would like it to format with the commas eg 1,500 not 1500. I know this is probably really basic but I'm still learning.. any help would be much appreciated. Cheers

functions.php

function wpb_total_posts() { 
$total = wp_count_posts()->publish;
echo '' . $total;
} 

Header.php

<?php wpb_total_posts(); ?>

1 Answer 1

1

You can use the PHP function number_format().

function wpb_total_posts() { 
    $total = wp_count_posts()->publish;
    echo number_format( 
        $total, // your number
        0,      // number of decimal points
        '.',    // decimal point separator
        ','     // thousands separator
    );
} 

Or, because you are using the default values anyway, you can shorten the function to:

function wpb_total_posts() { 
    echo number_format( wp_count_posts()->publish );
} 
1
  • Thank you so so much legend!! Perfect!
    – Jason
    Commented Oct 4, 2020 at 15:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.