1

Below is a function that displays the latest 4 approved comments on my homepage, I wrote this almost a year ago, and for the life of me, it's gone completely from my head, All i want to do is add a permalink to where the comment is located. How do I, and where do I add this?

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 4";  
 $comments = $wpdb->get_results($sql);  
 $output .= '<img src="/wp-content/themes/blue-and-grey/images/student-perspectives.jpg" /><h2>Latest student perspectives</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
0

Make use of the get_comment_link function in your foreach loop like so:

foreach ($comments as $comment) { 
    $output .= '<li><strong>'
            . $comment->comment_author
            . ' said</strong> : "'
            . '<a href="'
            . get_comment_link( $comment->comment_post_ID )
            . '">'
            . strip_tags($comment->com_excerpt)
            . '..."</a></li>';
}
| improve this answer | |
  • this doesn't work, it just strings aload of random urls together that aren't actually close to the links of the comments, is their no way of getting a simple permalink or post ID of comment other than the way above? – user1077369 Sep 27 '13 at 16:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.