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());
}
}
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());
}
}
Share
Improve this question
edited Jun 10, 2017 at 9:34
Johansson
15.4k11 gold badges43 silver badges79 bronze badges
asked Jun 10, 2017 at 9:24
Hank ScorpioHank Scorpio
1592 gold badges2 silver badges12 bronze badges
2 Answers
Reset to default 1Yes 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';
}
}
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
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742360632a4429324.html
评论列表(0条)