Bootstrap Templates Bundle

Tuesday, May 7, 2013

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:

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

Add this where you want to show page view

<?php echo getPostViews(get_the_ID()); ?>



Popular post: for all categories
 <?php

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(); ?>
               
                <li class="liHeadList"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
                <?php
endwhile; endif;
wp_reset_query();
?>



popular post: for all category.php 

<?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(); ?>              
                <li class="liHeadList"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>              
                <?php
endwhile; endif;
wp_reset_query();
?>

1 comment:

Wokiee React eCommerce Template