CUSTOM SHOW POST USIN WORDPRESS






Get post view without any plugin in WordPress



Get post view without any plugin in WordPress


Follow the steps given bellow


  1. Go to your theme’s function.php and add the following code in 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”;
    }
    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_action( ‘wp_head’, ‘adjacent_posts_rel_link_wp_head’, 10, 0);
  1. This step tracking the view of the post so post the following code in single.php where you want to display the post views.
setPostViews(get_the_ID());
?>

3 Comments

  1. Nice knowledge gaining article. This post is really the best on this valuable topic. Brizy review

    ReplyDelete
  2. Press organization Reuters utilizes WordPress to stay up with the latest with the most recent news as it occurs. Music gushing administration Spotify powers its whole site with WordPress. neuronto deepl wordpress plugin

    ReplyDelete