paste this below code into functions.php file
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, '1');
return "1 জন পোষ্টটি দেখেছেন";
}
return $count.' জন পোষ্টটি দেখেছেন';
}
function setPostViews($postID) {
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count==''){
$count = 1;
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '1');
}else{
$count++;
update_post_meta($postID, $count_key, $count);
}
}
Then add this where you want normally in post-sigle.php file
<?php
echo getPostViews(get_the_ID());
?>
and if you want to show the popular post list then add this where you want to show popular post list
<ul>
<?php
global $post;
$args = array( 'numberposts' => 5, 'offset'=> 1, 'category' => 1 );
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<li><?php setPostViews(get_the_ID()); echo getPostViews(get_the_ID());; ?></li>
<?php endforeach; ?>
Source: http://stackoverflow.com/questions/9472602/wordpress-popular-posts-without-using-plugins
No comments:
Post a Comment