Comment Function

时间:2012-09-11 作者:Owen O\'Neill

如何使此函数仅显示最新的5条注释?

function showLatestComments() {
  global $wpdb;  
  $sql = "
   SELECT DISTINCT comment_post_ID, comment_author, comment_date_gmt, comment_approved, SUBSTRING(comment_content,1,100) AS com_excerpt 
   FROM $wpdb->comments 
   WHERE comment_approved = \'1\'
   ORDER BY comment_date_gmt DESC 
   LIMIT 5";  
 $comments = $wpdb->get_results($sql);  
 $output .= \'<h2>Latest student perspectives <img src="/wp-content/themes/blue-and-grey/images/hmepage-tri.png" class="class3" /></h2>
 <ul id="comm">\';  
 foreach ($comments as $comment) { 
   $output .= \'<li><strong>\'. $comment->comment_author . \' said</strong> : "\' . strip_tags($comment->com_excerpt). \'..."</li>\';
   }
 $output .= \'</ul>\';  
 echo $output;  
}//end function

1 个回复
SO网友:Bainternet

您发布的函数被限制为5条注释,这意味着它应该可以工作,但不管怎样,当您可以使用本机get\\u comments函数时,不需要自定义SQL查询

function showLatestComments() {
    $comments = get_comments(array(
        \'status\' => \'approve\',
        \'number\' => \'5\'
        )
    );
    $output .= \'<h2>Latest student perspectives <img src="/wp-content/themes/blue-and-grey/images/hmepage-tri.png" class="class3" /></h2>
    <ul id="comm">\';  
    foreach ($comments as $comment) { 
        $output .= \'<li><strong>\'. $comment->comment_author . \' said</strong> : "\' . substr(strip_tags($comment->content),0,99). \'..."</li>\';
    }
    $output .= \'</ul>\';  
    echo $output;  
}//end function

结束

相关推荐

Why are my comments closed?

我有一个简单的site 有几页加上blog. 出于某种原因,博客帖子声明comments are closed 我不知道为什么。我想打开所有博客帖子上的评论(而不是在页面上提供评论)。在“编辑帖子”屏幕中,“评论”部分仅表示“还没有评论”但并没有提供任何打开它们的方法。在“设置”>“讨论”中,我有以下内容:评论时,作者必须填写姓名和电子邮件。评论时,用户必须注册并登录。评论时,关闭对超过(14)天的文章的评论。自动关闭评论。启用线程化(嵌套)评论级别(5)深度。将评论分成每页有(50)条顶级评论的页