Here is the code...
if ( !function_exists( 'getCrunchifyPostViews' ) ) {
function getCrunchifyPostViews($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';
}
}
if ( !function_exists( 'setCrunchifyPostViews' ) ) {
function setCrunchifyPostViews($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);
}
}
}
if ( !function_exists( 'mvp_post_views' ) ) {
function mvp_post_views(){
$post_id = get_the_ID();
$count_key = 'post_views_count';
$n = get_post_meta($post_id, $count_key, true);
if ($n > 999999999) {
$n_format = number_format($n / 1000000000, 1) . 'B';
} else if ($n > 999999) {
$n_format = number_format($n / 1000000, 1) . 'M';
} else if ($n > 999) {
$n_format = number_format($n / 1000, 1) . 'K';
} else {
$n_format = $n;
}
echo $n_format;
}
}
it works fine using this php code to show views <?php mvp_post_views(); ?>
Thou would like to show views using specified posts ID...
Here is the code...
if ( !function_exists( 'getCrunchifyPostViews' ) ) {
function getCrunchifyPostViews($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';
}
}
if ( !function_exists( 'setCrunchifyPostViews' ) ) {
function setCrunchifyPostViews($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);
}
}
}
if ( !function_exists( 'mvp_post_views' ) ) {
function mvp_post_views(){
$post_id = get_the_ID();
$count_key = 'post_views_count';
$n = get_post_meta($post_id, $count_key, true);
if ($n > 999999999) {
$n_format = number_format($n / 1000000000, 1) . 'B';
} else if ($n > 999999) {
$n_format = number_format($n / 1000000, 1) . 'M';
} else if ($n > 999) {
$n_format = number_format($n / 1000, 1) . 'K';
} else {
$n_format = $n;
}
echo $n_format;
}
}
it works fine using this php code to show views <?php mvp_post_views(); ?>
Thou would like to show views using specified posts ID...
Share Improve this question asked May 31, 2019 at 0:48 John4PeaceJohn4Peace 31 bronze badge 1 |1 Answer
Reset to default 0You can pass post id as argument in the views function. Please check following example. An optional argument is passed in the function. If nothing is passed, id is fetched from get_the_ID()
.
if ( !function_exists( 'mvp_post_views' ) ) {
function mvp_post_views( $post_id = '' ){
if ( $post_id ) {
$post_id = absint( $post_id );
} else {
$post_id = get_the_ID();
}
$count_key = 'post_views_count';
$n = get_post_meta($post_id, $count_key, true);
if ($n > 999999999) {
$n_format = number_format($n / 1000000000, 1) . 'B';
} else if ($n > 999999) {
$n_format = number_format($n / 1000000, 1) . 'M';
} else if ($n > 999) {
$n_format = number_format($n / 1000, 1) . 'K';
} else {
$n_format = $n;
}
echo $n_format;
}
}
if ($count=='')
should beif (!$count)
asget_post_meta
returns false not an empty string if the key is not set. otherwise seems okay. – majick Commented May 31, 2019 at 4:08