Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /* Visit Counter For WordPress by Talkofweb.com */
- 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);
- }
- }
- ?>
- paste this after the_content();. The below code will count for every visit using the setPostViews(); function.
- <?php setPostViews(get_the_ID());?>
- <?php echo getPostViews(get_the_ID());;?>
- Getting the most popular WordPress post Loop using the Visits Counter:
- <h3>Popular Stuff:</h3>
- <?php
- $popularpost = new WP_Query( array( 'posts_per_page' => 4, 'meta_key' => 'post_views_count', 'orderby' => 'meta_value_num', 'order' => 'DESC' ) );
- while ( $popularpost->have_posts() ) : $popularpost->the_post();?>
- <li>
- <a href="<?php the_permalink() ?>" title="<?php the_title();?>"><h1><?php the_title();?></h1></a>
- </li>
- <?php endwhile; ?>
- <?php wp_reset_query(); ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement