Add post views count to theme

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #5701 Reply
    Ali.Dbg
    Guest

    add code to “wp-content/themes/frontier/function.php”

    // function to display post views count by Ali.Dbg
    function postviews() {
     $views_str = " Views ";
     $count_key = 'views';
     $postID = get_the_ID();
     $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');
    	echo $count.$views_str;
     }else{
    	if (is_single($postID)){
    	$count++;
    	update_post_meta($postID, $count_key, $count);
    	echo $count++.$views_str;
    	}else{
    	echo $count.$views_str;}
     }
    }
    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'] = __('Post Views'); return $defaults;}
    function posts_custom_column_views($column_name, $id){if($column_name === 'post_views'){postviews();}}

    add code to line 33 loop-blog.php – v1.2

    	<?php if ( get_post_type() == 'post'): ?>
    		<div class="entry-comment-info">
    			<i class="genericon genericon-show"></i><a href=""><?php postviews();?></a>
    		</div>
    	<?php endif; ?>
    

    add code to line 36 loop-single.php (v1.2)

    	<?php if ( is_singular('post') ): ?>
    		<div class="entry-comment-info">
    			<i class="genericon genericon-show"></i><a href=""><?php postviews();?></a>
    		</div>
    	<?php endif; ?>
    #5704
    Ron
    Keymaster

    You’ll have to add your functions on a child theme functions.php because they won’t be added to the theme.

Viewing 2 posts - 1 through 2 (of 2 total)
Reply To: Add post views count to theme
Your information: