簡體   English   中英

在評論模板之外顯示帖子的第一條評論(也許使用SQL)

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

在Wordpress中,如何在注釋表之外查找並顯示第一個注釋或管理員的注釋?

我想象像一個顯示該帖子的第一條評論的sql查詢...

有什么線索嗎? 謝謝!

此功能可獲取所有帖子的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 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

我認為“第一篇文章”是最早的評論。 一種簡單的方法是更改​​順序(因此,前面的注釋會優先出現),然后移至第一行。

$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";  

如果您這樣做,僅需要更改這兩行。

 ORDER BY comment_date_gmt ASC 
 LIMIT 1";  

我對WordPress的數據庫結構不熟悉,但基本上,您將必須獲取當前帖子的ID,並將其作為WHERE參數傳遞。

我假設comment_post_ID跟蹤添加評論的帖子的ID? 如果是這樣的話:

$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";  

這將顯示您通過ID傳遞的5條最新評論

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM