简体   繁体   中英

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?

I imagine like an sql query that displays the first comment of the post...

Any clues? Thank you!

This is a function that gets 5 recent comments of all posts. 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.

I assume comment_post_ID keeps track of the ID of the post the comment was added to? 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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