Show Popular Post For Blog

<?php
query_posts(‘meta_key=post_views_count&orderby=meta_value_num&order=DESC&posts_per_page=>3′);
if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php $status = get_post_meta($post->ID,’blog_select’,true );?>
<?php if($status == ‘image’) {
?>

ID,’post_img’,true );?>”/>

ID,’s_desc’,true );?>

</div>
<?php
}
endwhile; endif;
wp_reset_query();
?>

and In single.php page add this—

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

In functions .php file add this

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

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: