Hit counter on your wordpress blog posts

Hit counter in the blog posts

Hit counter » hit counter for wordpress easyAs always when we are going to work with PHP functions the first thing we will do is to create a child theme. In our blog you will find this post where we explain how to do it.

Then, from your file manager or from appearance/theme editor paste the following PHP code in the functions.php file of your child theme, always inside the PHP opening tag:

if ( ! function_exists( 'function_name_given' ) ) :
/**     * get the value of view.     */
function function_name_given($postID) {
$count_key = 'wpb_post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count ==''){
$count = 1;
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '1');
} else {
$count++;
update_post_meta($postID, $count_key, $count);
}
}
endif;

The next thing we will do is duplicate the single.php file from your parent theme into your child theme. It’s as easy as doing a copy/paste from the file manager. And inside this file we will paste the following code inside the loop.

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

Finally we paste this code in the single.php file inside the page structure exactly where we want it to be displayed, customizing the text that goes before and after the numeric value of the counter.

<p> Esta entrada se la visitado;
<?php
if ( get_post_meta( get_the_ID() , 'wpb_post_views_count', true) == '') {echo '0' ;
} else {
echo get_post_meta( get_the_ID() , 'wpb_post_views_count', true); };?>
 veces.</p>

Check the following video for a better understanding of the steps.

Leave a Comment