How to Display Post Views in MyThemeShop Themes

Recently I started working on a custom design for this website, using the SocialMe theme from MyThemeShop. While I was looking around in the code, to see what does what and alter it to match my needs, I found a script, that collects page views for each post separately. Although, they are not used anywhere. This might be for a future feature from MyThemeShop, not sure. If you happen to know they are there for, please let me know in the comments down below.

How to Display Post Views in MyThemeShop Themes

After tinkering around with it for a while, I had the idea of creating a function, that can display these post views now that they are already there and being collected. So in this article, I’m going to show you how to display post views in MyThemeShop themes. If you’re not using a MyThemeShop theme, then check this post on how to display post views in WordPress.

Let’s get started with the tutorial, shell we?

Creating a function to retrieve the post views

First off, the post views are stored under two different names in the database for whatever reason. One of them is _mts_view_count and the second on is _wpt_view_count . Both store the same value and can be found in the wp_postmeta table.

Now that we have the names of the post views, we can create our function to retrieve them and display them. We’re going to use the  _mts_view_count version. Since this is a theme related modification, it can go into the functions.php

function andorwp_post_views() {
    // Retrieving the Views
    $views = get_post_meta( get_the_ID(), '_mts_view_count', true );
    
    // Check if there are any views collected.
    if ( ! empty( $views ) ) {
        echo $views . ' Post Views';
    }
}

Displaying the Post Views in socialMe

Okay, we have our function get and display the views. As for where to display them, that is completely up to you. For the sake of this tutorial, I’m going to show you, how can you display them in the header post info section of the socialMe theme.

For this, open up the single.php and look for line 55, where you should see the code that displays the comment counts. Just above that, add the following code:

<div class="post-views">
    <span><i class="fa fa-eye"></i> <?php andorwp_post_views($post_id); ?></span>
</div>

Check the image below to see where it should go:

As you can see, we added a custom class as well, called post-views . So we need to modify the style.css a bit too.

Open up the theme’s style.css and around line 696 look for:

.post-info .thecomment {
    font-weight: 600;
    text-transform: uppercase;
}
.latestPost .post-info .thecomment, .single_post .post-info .thecomment {
    margin-right: 0;
    vertical-align: middle;
    display: table-cell;
    text-align: right;
}

And replace it with:

.post-info .thecomment,
.post-info .post-views {
    font-weight: 600;
    text-transform: uppercase;
}
.latestPost .post-info .thecomment,
.single_post .post-info .thecomment,
.single_post .post-info .post-views {
    margin-right: 0;
    vertical-align: middle;
    display: table-cell;
    text-align: right;
}

This won’t change anything on how the theme looks, we are just adding our custom class which is used to display the post views, to have the same styling as the comment counts.

If you change the CSS and add the function as well, then when you look at a single post, and you have the header options enabled, you should see something like this:

And that’s it! You have you post views displayed.

Add custom theme option to enable/disable post views

If you want to take this a step further, you can add a custom theme option so you can enable/disable this at any given time. Let’s see how.

First off, we need to change how we display the views. We need to wrap our HTML in the single.php into a PHP IF/Else clause, to check if it’s enabled or disabled.

<?php if ( isset ( $mts_options['mts_single_meta_info_enable']['post_views'] ) == '1' ) { ?>
    <div class="post-views">
        <span><i class="fa fa-eye"></i> <?php andorwp_post_views($post_id); ?></span>
    </div>
<?php } ?>

Then, we have to add our custom theme option. Open the theme’s theme-options.php file, go to line 825 where you’ll find a section for “Single-Page Post Meta Info“. Look for the options array, and extend it so it looks like this:

'options' => array(
    'author_image'=> __('Enable Author Image', 'socialme' ), 
    'author' => __('Enable Author Name', 'socialme' ), 
    'post_views' => __('Enable Views', 'socialme' ), 
    'post_date' => __('Enable Date', 'socialme' ), 
    'comment' => __('Enable Comment Count', 'socialme' )
),

by adding 'post_views' => __('Enable Views', 'socialme' ),  into the array.

Now that we added the new post_views option when you go to the theme options -> single posts, you should see a new option appear. A checkbox.

And at this point, if you did everything right, modifying the HTML in the single.php and adding the new option to the theme options array, you should be good to go.

If found this tutorial useful in any shape or form, feel free to share it with your followers and friends! Also if u have any questions or suggestions, feel free to leave a comment down below! And don’t forget to subscribe below for future tutorials like this one!

Get more stuff like this

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for subscribing.

Something went wrong.

Add Comment

Get more stuff like this
in your inbox

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for subscribing.

Something went wrong.