Display Most Commented Posts In WordPress
A popular feature to add to your wordpress blog commonly in the sidebar is to display your blogs popular posts.
There are main plugins that can do this but I prefer not to use plugins as it is quick and easy to just add this into your page and then you can style it exactly as you want.
WordPress Code To Add Popular Posts
The below snippet will run a SQL query to get the most popular post ordered by the posts with the most comments. You can also define how many posts you get back, in this example I am returned 10 posts ordered by the most commented. You can then get the result of the query and loop through this and display the posts in whatever way you want.
In this example I am just adding the posts to a list item and displaying the title of the post, the title will link to the post to make it easy for the visitors to see your popular posts. Under the title I am displaying the comment count so the visitor will be able to see how popular this post is.
Just add this in your theme where ever you want the popular posts to be displayed.
<ul class="popular_posts"> <?php $pc = new WP_Query('orderby=comment_count&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 comments_popup_link('No Comments;', '1 Comment', '% Comments'); ?></p></li> <?php endwhile; ?> <?php wp_reset_postdata(); ?> </ul>
Fastest WordPress Hosting With WPEngine
Stunning speed, powerful security, and best-in-class customer service. At WP Engine.
- Access 100s of premium tutorials and downloadable content
- Members content consists of premium WordPress plugins
- CSS packages, jQuery packages, tutorial demo files and templates for 100s of web development tutorials
- In-depth development tutorials
- Priority tutorial requests
- No ads