1

I wanted to know if i can change the way i display views count in WordPress.

Example: 1000 views = 1k - 10000 views = 10k

I'm counting and viewing post views by using this code:

// Count views
function setPostViews($postID) {
  $count_key = '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);
  }
}

// Show views
function getPostViews($postID) {
  $count_key = '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 View";
  }
  return $count.' Views';
}

// Show views in WP-Admin
add_filter('manage_posts_columns', 'posts_column_views');
add_action('manage_posts_custom_column', 'posts_custom_column_views', 5, 2);
function posts_column_views($defaults) {
  $defaults['post_views'] = __('Views');
  return $defaults;
}
function posts_custom_column_views($column_name, $id){
  if($column_name === 'post_views') {
    echo getPostViews(get_the_ID());
  }
}

2 Answers 2

1

Yes you can. You have to check if the post view count is higher than 1000, is so, then round it and return it:

function getPostViews($postID) {
    $count_key = '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 View";
    }
    if ($count > 1000) {
        return round ( $count / 1000 ,1 ).'K Views';
    } else {
        return $count.' Views';
    }
}
0
0

I uses this function for a while and works great for my needs:

/**
 * 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;
}
}

now you just call the function like this example:

$views = getPostViews($postID);     
$views = short_number($views);
return $views;

I hope it helps anyone else in need :D

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.