如何显示 1.1k 等数字而不是 1100

我正在尝试在我正在构建的网站上显示 1.1k 而不是 1000 的帖子浏览量。这是一个 Wordpress 网站,我在尝试向该网站添加自定义功能时遇到了麻烦。每次我添加一些我在这里找到的片段时,它都会使网站完全崩溃。


这是目前的代码 - 它只是计算登陆该帖子的用户以及管理员能够手动设置帖子计数。


    if(!function_exists('davenport_getPostViews')):

function davenport_getPostViews($postID){

    $count_key = '_davenport_post_views_count';

    $count = get_post_meta($postID, $count_key, true);

    if($count == ''){

        delete_post_meta($postID, $count_key);

        add_post_meta($postID, $count_key, '0');

        return 0;

    }

    return $count;

}

endif;


if(!function_exists('davenport_setPostViews')):

function davenport_setPostViews() {

    global $post;

    $postID = $post->ID;


    $count_key = '_davenport_post_views_count';

    $count = get_post_meta($postID, $count_key, true);

    if($count == '') {

        $count = 0;

        delete_post_meta($postID, $count_key);

        add_post_meta($postID, $count_key, '0');

    } else {

        $count++;

        update_post_meta($postID, $count_key, $count);

    }

}

add_action('davenport_set_post_views', 'davenport_setPostViews');

endif;

任何帮助将不胜感激,因为我正竭尽全力试图解决这个问题。


狐的传说
浏览 163回答 4
4回答

ibeautiful

一个简单的方法是将其分开。所以这样做看起来像这样:<?php&nbsp;function DisplayViews($views){&nbsp; &nbsp; if($views > 0){&nbsp; &nbsp; &nbsp; &nbsp; $display = round($views / 1000, 2);&nbsp; &nbsp; &nbsp; &nbsp; return $display."k";&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; return "0";&nbsp; &nbsp; }}echo DisplayViews($count); //$count should be your view count?>虽然上面的方法可以正常工作,但我建议您进行更多检查,以便在只有 10 个视图时不会显示 0.01k。当观看次数超过 999,999 次时也是如此。要进行这些检查,您需要执行以下操作:if($views <= 999){&nbsp; &nbsp; //Display number without letter "K"}if($views > 999999){&nbsp; &nbsp; //Display number with the letter "m"}因此,将这两者结合起来,您可以检查数字是否小于 100,还可以检查它是否大于 999,999,这样做意味着您不会显示末尾带有错误字母的数字。最终代码将如下所示:<?phpfunction DisplayViews($views){&nbsp; &nbsp; if($views > 0){&nbsp; &nbsp; &nbsp; &nbsp; if($views <= 999){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $views;&nbsp; &nbsp; &nbsp; &nbsp; } elseif($views > 999999){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $display = round($views / 1000000, 2);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $display."M";&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $display = round($views / 1000, 2);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $display."K";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; return "0";&nbsp; &nbsp; }}echo DisplayViews($count);?>

交互式爱情

function numberAbbreviation($number) {$abbrevs = array(12 => "T", 9 => "B", 6 => "M", 3 => "K", 0 => "");foreach($abbrevs as $exponent => $abbrev) {    if($number >= pow(10, $exponent)) {        $display_num = $number / pow(10, $exponent);        $decimals = ($exponent >= 3 && round($display_num) < 100) ? 1 : 0;        return number_format($display_num,$decimals) . $abbrev;    }}}

波斯汪

它类似于将字节大小转换为人类可读的格式。function getReadableCount($count, $dec = 2) {&nbsp; &nbsp; $units = ['K', 'M', 'B'];&nbsp; &nbsp; for ($i = count($units); $i > 0; $i --) {&nbsp; &nbsp; &nbsp; &nbsp; $base = pow(1000, $i);&nbsp; &nbsp; &nbsp; &nbsp; if ($count >= $base) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return round($count/$base, $dec) . $units[$i-1];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return $count;}echo getReadableCount($count, 1);

一只萌萌小番薯

在您的计数变量上调用此函数。function humanize_number($input){&nbsp; &nbsp; $input = number_format($input);&nbsp; &nbsp; $input_count = substr_count($input, ',');&nbsp; &nbsp; if($input_count != '0'){&nbsp; &nbsp; &nbsp; &nbsp; if($input_count == '1'){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return substr($input, 0, -4).'k';&nbsp; &nbsp; &nbsp; &nbsp; } else if($input_count == '2'){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return substr($input, 0, -8).'mil';&nbsp; &nbsp; &nbsp; &nbsp; } else if($input_count == '3'){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return substr($input, 0,&nbsp; -12).'bil';&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; return $input;&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP