2007
12.21
12.21
之前寫了一個 Plugin 用來讀取 Recents Comment ,叫做 roga’s recent comments – WordPress Plugin 。當時取出的 SQL Statements 只有考慮 comment_approved (這是有沒有被 moderate 與否) 以及 $wpdb->posts.post_status=’publish’ (應該是在 2.3 之前,資料庫結構沒改,只要設了 Password ,Post 的狀態就會變成 Private, 到了 2.3 之後,就算 Post 有設定 Password, Post_Status 依然會是 Published.)
所以,我只好想個辦法,來補救這個問題。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
global $wpdb, $comments, $comment;
$sql = "SELECT ID,comment_ID,comment_post_ID, post_title, comment_date, comment_author, comment_content ";
$sql .= "FROM $wpdb->comments,$wpdb->posts WHERE $wpdb->posts.ID=$wpdb->comments.comment_post_ID ";
$sql .= "AND comment_approved='1' AND $wpdb->posts.post_status='publish' ORDER BY $wpdb->comments.comment_date DESC LIMIT $comment_num";
$comments=$wpdb->get_results($sql);
foreach ($comments as $comment)
{
//開始把結果 assign 給變數
} |
但是這樣會有一個很大的麻煩,就是選取 Recent Comments 的時候,會把被 Protected 的 Comments 一起 excerpt 出來。所以只好再多加一個條件下去查詢。
|
1 |
AND $wpdb->posts.post_password='' |
// 在 WHERE 後面把這個條件一起 AND 進去就可以了。
另外, WordPress 內建的 Widget (Recent Comment)很巧妙的避掉了這個問題,因為他只會讀取標題,而標題就是 Protect: Title ,所以不會顯示 Comment 內文。




No Comment.
Add Your Comment