admin管理员组

文章数量:1122846

Can anyone please walk me through how to make post views on Wordpress shorten to 10K or 1.1M rather than displaying the full number 10,000 or 1,000,000.

I found the code below but not sure how to implement it into the Wordpress child theme. Putting it into the functions.php file didn't do anything.

    /**
 * Shorten long numbers to K/M/B (Thousand, Million and Billion)
 *
 * @param int $number The number to shorten.
 * @param int $decimals Precision of the number of decimal places.
 * @param string $suffix A string displays as the number suffix.
 */
if(!function_exists('short_number')) {
function short_number($n, $decimals = 2, $suffix = '') {
    if(!$suffix)
        $suffix = 'K,M,B';
    $suffix = explode(',', $suffix);

    if ($n < 1000) { // any number less than a Thousand
        $shorted = number_format($n);
    } elseif ($n < 1000000) { // any number less than a million
        $shorted = number_format($n/1000, $decimals).$suffix[0];
    } elseif ($n < 1000000000) { // any number less than a billion
        $shorted = number_format($n/1000000, $decimals).$suffix[1];
    } else { // at least a billion
        $shorted = number_format($n/1000000000, $decimals).$suffix[2];
    }

    return $shorted;
}
}

本文标签: functionsAbbreviate Wordpress post view numbers