Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
// this goes in functions.php in your theme. Then you can use shortcode [most_commented] in posts and pages
// to get the top 150 posts
function mostCommented() {
echo '<p>';
$posts_per_page = 150;
query_posts('orderby=comment_count&posts_per_page='.$posts_per_page.'&order=DESC');
if ( have_posts() ) : while ( have_posts() ) : the_post();
echo '<a href="';
the_permalink();
echo '" rel="bookmark" title="';
the_title_attribute('','',false);
echo '">';
the_title();
echo '</a>';
echo ' <span title="Number of comments: ';
comments_number('0','1','%');
echo '">(';
comments_number('0','1','%');
echo ')</span> <br />';
echo "\n";
endwhile;
else :
echo '<p>Sorry, no posts were found.</p>';
endif;
echo '</p>';
// reset the query so when I use it it doesn't
// cause posts to be displayed
query_posts('tag=NOTAREALTAG');
return $out;
}
add_shortcode('most_commented', 'mostCommented');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.