代码如下 | 复制代码 |
<div class="recent-comments"> <h3>最新评论</h3> <ul> <?php $show_comments = 10; //评论数量 $my_email = "loveyouf@vip.qq.com"; //获取博主自己的email $i = 1; $comments = get_comments('number=200&status=approve&type=comment'); //取得前200个评论,如果你每天的回复量超过200可以适量加大 foreach ($comments as $rc_comment) { if ($rc_comment->comment_author_email != $my_email) { ?> <li><dl><dt><?php echo get_avatar($rc_comment->comment_author_email,24); ?><?php echo $comment_author; ?></dt><dd><h5><a href="<?php echo get_permalink($rc_comment->comment_post_ID); ?>#comment-<?php echo $rc_comment->comment_ID; ?>"><?php echo mb_strimwidth(convert_smilies($rc_comment->comment_author.":".$rc_comment->comment_content),0,45,"..."); ?></a></h5></dd></dl></li> <?php if ($i == $show_comments) break; //评论数量达到退出遍历 $i++; } // End if } //End foreach ?> </ul> </div> |
其中第6句用get_bloginfo函数不知道为什么获取不到邮箱字段,懒得研究了,直接填邮箱,用mb_strimwidth函数实现了截断,D2默认的评论调用方式如下。。。
templates/recentcomment.php
代码如下 | 复制代码 |
<?php global $wpdb; $sql = "SELECT DISTINCT ID, post_title, post_password, comment_ID, comment_post_ID, comment_author, comment_date_gmt, comment_date, comment_approved,comment_author_email, comment_type,comment_author_url, SUBSTRING(comment_content,1,12) AS com_excerpt FROM $wpdb->comments LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID) WHERE comment_approved = '1' AND comment_type = '' AND comment_author != 'admin' AND post_password = '' ORDER BY comment_date_gmt DESC LIMIT 10"; $comments = $wpdb->get_results($sql); $output = $pre_HTML; foreach ($comments as $comment) { $comment_author = strip_tags($comment->comment_author); $comment_content = strip_tags($comment->com_excerpt); $permalink = get_permalink($comment->ID)."#comment-".$comment->comment_ID; $post_title = $comment->post_title; $comment_date = $comment->comment_date; $email = $comment->comment_author_email; $output .= '<li><dl><dt>'.get_avatar($email, 24).'</dt><dd><h5>'.'<a href="'.$permalink.'" title="'.$post_title.'">'.$comment_content.'»'.'</a></h5><small class="en">'.'Post: '.$comment_date.'</small></dd></dl></li>' ; } $output .= $post_HTML; $output = convert_smilies($output); echo $output; ?> </ul> |
templates/sidebar.php
代码如下 | 复制代码 |
<div class="recent-comments"> <h3>最新评论</h3> <?php include('templates/recentcomment.php'); ?> </div> |
时间: 2024-09-21 13:27:05