简体   繁体   English

在评论模板之外显示帖子的第一条评论(也许使用SQL)

[英]Display first comment of post outside the comments template (using SQL maybe)

in Wordpress, how can I find and display the first comment or the comment of an administrator outside of the comments table? 在Wordpress中,如何在注释表之外查找并显示第一个注释或管理员的注释?

I imagine like an sql query that displays the first comment of the post... 我想象像一个显示该帖子的第一条评论的sql查询...

Any clues? 有什么线索吗? Thank you! 谢谢!

This is a function that gets 5 recent comments of all posts. 此功能可获取所有帖子的5条最近评论。 I would like to edit this and show the latest of the post I am reading 我想对此进行编辑,并显示我正在阅读的最新文章

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 comments</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

I take it that "first post" means the earliest comment. 我认为“第一篇文章”是最早的评论。 One simple way is to change the order (so earlier comments come first), then take the first row. 一种简单的方法是更改​​顺序(因此,前面的注释会优先出现),然后移至第一行。

$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 ASC 
 LIMIT 1";  

Only these two lines need to be changed if you do it this way. 如果您这样做,仅需要更改这两行。

 ORDER BY comment_date_gmt ASC 
 LIMIT 1";  

I'm not familiar with the database structure of WordPress, but basicly you would have to get the ID of the current post and pass that along as a WHERE parameter. 我对WordPress的数据库结构不熟悉,但基本上,您将必须获取当前帖子的ID,并将其作为WHERE参数传递。

I assume comment_post_ID keeps track of the ID of the post the comment was added to? 我假设comment_post_ID跟踪添加评论的帖子的ID? Ifso: 如果是这样的话:

$id = mysql_escape_string($_GET["id"]); // get id of post somewhere
  $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' AND comment_post_ID = {$id}
   ORDER BY comment_date_gmt DESC 
   LIMIT 5";  

That would show the 5 most recent comments to the post of which you pass along the id 这将显示您通过ID传递的5条最新评论

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM