posts WHERE 1 ORDER BY comment_count DESC LIMIT 0 , 3");foreach ($resul" />

从数据库(SQL)中选取评论数量最多的3篇文章,作为热门文章

以评论数量作为热门文章是一种常见用法:

<?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 the_category(","); ?>  <?php post_views(); ?>   作者<?php echo get_avatar( get_the_author_meta( 'user_email' ) ); ?><?php the_author(); ?>   <?php the_time('m-d H:s'); ?>
  • <?php
    }
    }
    ?>

    代码解释:利用 $result = $wpdb->get_results(“”) 取得查询的结果集,实际上是一个多维数组,然后foreach循环读取每一行数据到$topten变量,最后用$topten->post_title取得标题的值。 $result = $wpdb->get_results(“”) 括号里面的是SQL查询语句.

    #现在前往

    精选留言

    文章,数据库,SQL,选取
    sample
    2021-02-28
    写留言
    签到
    投稿
    QQ咨询
    返回顶部