In wordpress blog, wordpress provide the option to comment on the post and that would be posted on the blog after admin approval. Many of us will be using any number of plugins or the database query that’s been doing the rounds recently to display your most commented posts. But we can achieve it simply by using WordPress’s query_posts or WP_query().
By using query_posts() :
1 2 3 4 5 6 7 8 9 10 11 12 | <ul> <?php query_posts('orderby=comment_count&posts_per_page=>5'); if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> <li> <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"> <?php the_title(); ?> (<?php comments_number('0','1','%'); ?>)</a> </li> <?php endwhile; ?> <?php else : ?> <li>Sorry, no posts were found.</li> <?php endif; ?> </ul> |
By using WP_query()
1 2 3 4 5 6 7 | <ul> <?php $popular = new WP_Query('orderby=comment_count&posts_per_page=5'); ?> <?php while ($popular->have_posts()) : $popular->the_post(); ?> <li> <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?> (<?php comments_number('0','1','%'); ?>)</a> </li> <?php endwhile; ?> </ul> |
We can use one of both the way to get the most commented posts on our blog. Here “posts_per_page” option gives the number of posts to display.