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
版权声明:本文标题:functions - Abbreviate Wordpress post view numbers 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736309635a1934088.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论