0815

Du musst dich anmelden um Beiträge und Themen zu erstellen. Einloggen · Registrieren

Display Most Commented Posts without a Plugin

Here is another trick that will enable you avoid yet another needless plugin. This code results in the display of your most-commented posts in list format:

<ul>
<?php 
$result = $wpdb->get_results("SELECT comment_count, ID, post_title FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , 10"); // NUMBER OF POSTS
foreach ($result as $topten) {
	$postid = $topten->ID;
	$title = $topten->post_title;
	$commentcount = $topten->comment_count;
	if ($commentcount != 0) {
?>

<li><a href="<?php echo get_permalink($postid); ?>"><?php echo $title ?></a></li>

<?php } } ?>
</ul>

This code may be modified to display any number of posts by changing the LIMIT from “10” to whatever you wish. You may also change the display order from DESC (descending order) to ASC(ascending order).

Once in place, this code will display a list of the 10 most-commented posts in the following (X)HTML output format:

<ul>
<li><a href="http://domain.tld/post-01/">Post Title 01</a></li>
<li><a href="http://domain.tld/post-02/">Post Title 02</a></li>
<li><a href="http://domain.tld/post-03/">Post Title 03</a></li>
.
.
.
</ul>

Source: StylizedWeb (404 link removed 2015/03/12)




0