Display Post View Count In Wordpress Without Plugin

Add Tool

Display Post View Count In Wordpress Without Plugin


Display Post View Count In Wordpress Without Plugin

WordPress post views are very important for a blogger. It helps to know which posts or pages are most popular for our user or visitors. It helps to know user demand also. Personally I am using it for my site. You will see the live demo of post views under my post title in home, category and single pages. Here you will see number views (100 views). To add this post views option in your WordPress site is not hard. It need some PHP codes that will be placed within your functions.php, single.php and where you want to display. So follow the codes below and do it.

wordpress post views

Step-1: Now open your functions.php from Admin panel – Appearance – Editor and find “functions.php” from the right panel of the editor page and past the codes below under the functions codes.

<?php

////////////////////////////////////////////////////

// function to display number of posts 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';

}

// function to 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);

}

}

// Add it to a column 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());

}

}

?>

Step-2: Now open your single.php from Admin panel – Appearance – Editor and find “single.php” from the right panel of the editor page and past the codes.

<?php setPostViews(get_the_ID()); ?>

You must be pasted the code within the loop like

<?php if(have_posts()) : ?>
<?php setPostViews(get_the_ID()); ?>
<?php while(have_posts()) : the_post(); ?>

The above codes will active your post views count. Now we need to display in our site.

Step-3: To display the post views past the codes below where you want.

<?php echo getPostViews(get_the_ID()); ?>

I like to display post views under my post title so I placed it within my index.php file and single.php file such as

<span><?php the_date('j F, Y');?></span>
<span><?php echo getPostViews(get_the_ID()); ?></span>

Here,

<span><?php the_date('j F, Y');?></span>

Will display post submitted date and

<?php echo getPostViews(get_the_ID()); ?>

Will display post views?

If this article is helpful please share it with you friends and if you have some another codes to develop site, you can share with us through blog comment or blog post. Thanks for reading the article.


Tags: wordpress post view count without plugin, wordpress post view stats, wordpress single post view, wordpress admin view, wordpress display posts, wordpress view post revisions, post view plugin wordpress, wordpress post views
Payza

About Masum Billah

Display Post View Count In Wordpress Without Plugin 1stWebSuite website seo tool is a free website ranking tool site provides a lot of on page and off page seo useful tools for you. On this site, we have a large number of website ranking tools


SIGN UP TO GET UPDATE TO YOUR INBOX




Discuss about: Display Post View Count In Wordpress Without Plugin


adf.ly