Skip to content

Instantly share code, notes, and snippets.

@kamalinfo
Last active December 28, 2017 05:33
Show Gist options
  • Save kamalinfo/e7c9347f179b2e630af44323265fb4d2 to your computer and use it in GitHub Desktop.
Save kamalinfo/e7c9347f179b2e630af44323265fb4d2 to your computer and use it in GitHub Desktop.
<?php
//this code is functions.php
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';
}
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);
}
}
// Remove issues with prefetching adding extra views
?>
<?php
///this code is single.php
setPostViews(get_the_ID());
?>
<!-- mfunc setPostViews(get_the_ID()); --><!-- /mfunc -->
<?php
//where show view
echo getPostViews(get_the_ID());
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment