Categories

Posts Tagged ‘最新评论’

wordpress显示最新评论代码

很多使用wordpress 的博客会在首页或者sidebar显示网友的最新评论, 其实这样的功能实现起来很简单,主要思路是从数据库中的评论数据表中取得最晚发布的N条记录即可。 代码在下面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
  <?php 
	global $wpdb; 
 
	$sql = &quot;SELECT DISTINCT ID, post_title, post_password, comment_ID, 
    comment_post_ID, comment_author, comment_date_gmt, comment_approved, 
    comment_type,comment_author_url, 
     SUBSTRING(comment_content,1,30) AS com_excerpt 
    FROM $wpdb-&gt;comments 
    LEFT OUTER JOIN $wpdb-&gt;posts ON ($wpdb-&gt;comments.comment_post_ID = 
    $wpdb-&gt;posts.ID) 
   WHERE comment_approved = '1' AND comment_type = '' AND 
   post_password = '' 
   ORDER BY comment_date_gmt DESC 
   LIMIT 10&quot;; 
 
   $comments = $wpdb-&gt;get_results($sql); 
   $output = $pre_HTML; 
   $output .= &quot;\n&lt;ul&gt;&quot;; 
   foreach ($comments as $comment) { 
	$output .= &quot;\n&lt;li&gt;&quot;. &quot;&lt;p&gt;&lt;a href=\&quot;&quot; . get_permalink($comment-&gt;ID) . 
		&quot;#comment-&quot; . $comment-&gt;comment_ID . &quot;\&quot; title=\&quot;on &quot; . 
		$comment-&gt;post_title . &quot;\&quot;&gt;&quot; . strip_tags($comment-&gt;com_excerpt) 
		.'&lt;/a&gt;('.strip_tags($comment-&gt;comment_author). ')&lt;/p&gt;&lt;/li&gt;'; 
   } 
   $output .= &quot;\n&lt;/ul&gt;&quot;; 
   $output .= $post_HTML; 
   echo $output;
   ?>