You are on page 1of 3

================================= Display popular posts by comments ================================= Displays all post, the post that has most comments

will be on the top of the lis t. <ul class="popular_posts"> <?php $pc = new WP_Query('orderby=comment_count&#038;posts_per_page=10') ; while ($pc->have_posts()) : $pc->the_post(); ?> <li><a href="<?php the_permalink(); ?>" title="<?php the_title() ; ?>"><?php the_title(); ?></a> <p>Posted by <strong><?php the_author() ?></strong> with <?php c omments_popup_link('No Comments;', '1 Comment', '% Comments'); ?></p></li> <?php endwhile; ?> </ul>

============================== Display popular posts by views ============================== www.devtoolsplus.com/wordpress-post-view-without-plugin/ Displays popular post based on how many times the post was viewed. Paste the code in Function.php //////////////////////////////////////////////////// // function to display number of posts views. /////////////////////////////////////////////////// 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 to 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); } }

// Add it to a column in WP-Admin add_filter('manage_posts_columns', 'posts_column_views'); add_action('manage_posts_custom_column', 'posts_custom_column_views',5,2); function posts_column_views($defaults){ $defaults['post_views'] = __('Views'); return $defaults; } function posts_custom_column_views($column_name, $id){ if($column_name === 'post_views'){ echo getPostViews(get_the_ID()); } }

Paste this code in the loop of the page to display the page views to set the pag e views count. <?php setPostViews(get_the_ID()); ?> EX: <?php if(have_posts()) : ?> <?php setPostViews(get_the_ID()); ?> <?php while(have_posts()) : the_post(); ?> My EX: <?php while ( have_posts() ) : the_post(); ?> <?php setPostViews(get_the_ID()); ?> <?php get_template_part( 'content', 'blogpost' ); ?> <?php billiant_designs_content_nav( 'nav-below' ); ?> <?php // If comments are open or we have at least one comment, load up the comment template if ( comments_open() || '0' != get_comments_number() ) comments_template(); ?> Paste the code to show the page views count. EX: In Footer.php <span><?php the_date('j F, Y');?></span> <span><?php echo getPostViews(get_the_ID()); ?></span> Output: 14 November, 2013 10 Views ANOTHER EX:

Displays list of popular posts the first has most views. Paste in footer or anywhere you want to display it. <?php $popularpost = new WP_Query( array( 'posts_per_page' => 4, 'meta_key' => 'post_v iews_count', 'orderby' => 'meta_value_num', 'order' => 'DESC' ) ); while ( $popularpost->have_posts() ) : $popularpost->the_post(); the_title(); getPostViews(get_the_ID()); endwhile; ?> Output: Blog Post Blog Post Blog Post Blog Post Three Eleven Ten Nine

You might also like