从数据库(SQL)中选取评论数量最多的3篇文章,作为热门文章
以评论数量作为热门文章是一种常见用法:
代码解释:利用 $result = $wpdb->get_results(“”) 取得查询的结果集,实际上是一个多维数组,然后foreach循环读取每一行数据到$topten变量,最后用$topten->post_title取得标题的值。 $result = $wpdb->get_results(“”) 括号里面的是SQL查询语句.
<?php
$result = $wpdb->get_results("SELECT comment_count,ID,post_title FROM $wpdb->posts WHERE 1 ORDER BY comment_count DESC LIMIT 0 , 3");
foreach ($result as $topten) {
$postid = $topten->ID;
$title = $topten->post_title;
$commentcount = $topten->comment_count;
if ($commentcount != 0) {
?>
<?php
}
}
?>
$result = $wpdb->get_results("SELECT comment_count,ID,post_title FROM $wpdb->posts WHERE 1 ORDER BY comment_count DESC LIMIT 0 , 3");
foreach ($result as $topten) {
$postid = $topten->ID;
$title = $topten->post_title;
$commentcount = $topten->comment_count;
if ($commentcount != 0) {
?>
}
}
?>
代码解释:利用 $result = $wpdb->get_results(“”) 取得查询的结果集,实际上是一个多维数组,然后foreach循环读取每一行数据到$topten变量,最后用$topten->post_title取得标题的值。 $result = $wpdb->get_results(“”) 括号里面的是SQL查询语句.
精选留言