Add Post Views Counter In WordPress Posts Without Plugin
I am not responsible if you done any mistake. So Check this video to Adding Code Though File Manager
Step 1: Adding Post Views Counter Code In Functions.PHP
- Go to Appearance > Editor
- Under Editor section Select Functions.php in the list of php files on the right of your screen (or just see image below)
- Paste Below code in functions.php
>// Display or Count how many times a post has been viewed.
// id = the post id and action = display or countfunction arixWp_PostViews( $id, $action ) {$axCountMeta = ‘ax_post_views’; // Your Custom field that stores the views
$axCount = get_post_meta($id, $axCountMeta, true);if ( $axCount == ” ) {if ( $action == ‘count’ ) {
$axCount = 0;
}delete_post_meta( $id, $axCountMeta );
add_post_meta( $id, $axCountMeta, 0 );if ( $action == ‘display’ ) {
echo “0 Views”;
}} else {if ( $action == ‘count’ ) {$axCount++;
update_post_meta( $id, $axCountMeta, $axCount );} else {echo $axCount . ‘ Views’;}}}
// id = the post id and action = display or countfunction arixWp_PostViews( $id, $action ) {$axCountMeta = ‘ax_post_views’; // Your Custom field that stores the views
$axCount = get_post_meta($id, $axCountMeta, true);if ( $axCount == ” ) {if ( $action == ‘count’ ) {
$axCount = 0;
}delete_post_meta( $id, $axCountMeta );
add_post_meta( $id, $axCountMeta, 0 );if ( $action == ‘display’ ) {
echo “0 Views”;
}} else {if ( $action == ‘count’ ) {$axCount++;
update_post_meta( $id, $axCountMeta, $axCount );} else {echo $axCount . ‘ Views’;}}}
- After adding above code Click Update File
Step 2: Showing Posts Views Counter In WordPress Posts
- Select Single.PHP from right of your screen (See image below)
- Now Paste below code just after High Lighted Section in above image.
- To display the count outside the loop use this:
- If you want them to show in the loop use this instead.
<?php
echo
arixWp_PostViews(
$post
->ID,
'display'
); ?>
- Finally Click on Update File and You had done.