popular post by category or category.php


Add this on functions.php:


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 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);
    }
}

The add this when the loop starts within single.php:



Add this where you want to show page view





Popular post: for all categories
 
query_posts('meta_key=post_views_count&orderby=meta_value_num&order=DESC&showposts=2&offset=4');
if (have_posts()) : while (have_posts()) : the_post(); ?>
               
               

  •                 endwhile; endif;
    wp_reset_query();
    ?>



    popular post: for all category.php 

    $category = get_the_category();
    query_posts('meta_key=post_views_count&orderby=meta_value_num&order=DESC&showposts=3&&category_name='.$category[0]->cat_name.'&cat=$category_id');
    if (have_posts()) : while (have_posts()) : the_post(); ?>              
                   
  •              
                    endwhile; endif;
    wp_reset_query();
    ?>