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.
Join the weekly newsletter
Sign up for our newsletter. You'll be updated on new tutorials as they come available.
Subscribe today and get a 50% discount code to Paulund