You are here: Home » All Categories » WordPress » WP Trick » 带头像显示的最新评论代码 - 完善篇

带头像显示的最新评论代码 - 完善篇

Dec 11th, 2009 13:04 | Leave a comment?(151) Go to comments

2010.7.5 Edit:最新的参考带头像显示的最新评论代码 - 蛋疼篇

因为昨天大修了主题的评论样式,从原来的 5 级嵌套改为 2 级嵌套,也保留了以前 5 级嵌套的现示,2级嵌套使用了 Jinwen 的思路,所以 2 级嵌套评论后的评论都是 @ 方式显示跟随在2级后门。

由此问题就来了,一直使用的最新评论代码不能显示或者转换带连接开头的评论内容,致使只要带有链接 @ 方式开头的评论内容显示不了,可 Jinwen 那里的最新评论可以显示带连接的评论内容。

我前几天请教过 Jinwen ,Jinwen说他用的是插件,于是只能自个折腾了,最后在国人开发的 WP Kit CN 这个插件找到了解决办法——加个截断函数。

下面我从头说起,来说明“最新评论代码”完善过程:

我在《用代码武装你的wordpress [part 1]》这篇文章贴过“最新评论代码 - Recent Comments”的代码,这是最基本的“最新评论”代码,支持中文摘录评论长度,代码如下:

<h2>Recent Comments</h2> <ul> <?php global $wpdb; $sql = "SELECT DISTINCT ID, post_title, post_password, comment_ID, comment_post_ID, comment_author, comment_date_gmt, comment_approved, comment_type,comment_author_url,comment_author_email, SUBSTRING(comment_content,1,16) 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 post_password = '' AND user_id='0' ORDER BY comment_date_gmt DESC LIMIT 10"; $comments = $wpdb->get_results($sql); $output = $pre_HTML; foreach ($comments as $comment) {$output .= "\n<li>".get_avatar(get_comment_author_email(), 32).strip_tags($comment->comment_author).":<br />" . " <a href=\"" . get_permalink($comment->ID) ."#comment-" . $comment->comment_ID . "\" title=\"on " .$comment->post_title . "\">" . strip_tags($comment->com_excerpt)."</a>...</li>";} $output .= $post_HTML; echo $output; ?> </ul>

说明:

  • comment_date_gmt DESC LIMIT 10 中的 10 是指要显示的评论个数
  • SUBSTRING(comment_content,1,16) 中的 16 是指每条评论的中文文字个数
  • ('comment_author_email'), 32) 中的 32 是指头像的图片大小

不要迷恋分割线,分割线只是个传说

一直用到上个月底才换为 willin 修改过的最新评论代码,我稍微改了一些如下:

<h2>Recent Comments</h2>
<ul class="recentcomments">
<?php
global $wpdb;
$sql = "SELECT DISTINCT ID, post_title, post_password, comment_ID, comment_post_ID, comment_author, comment_date_gmt, comment_approved, comment_type,comment_author_url,comment_author_email, SUBSTRING(comment_content,1,18) 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 != 'zwwooooo' AND post_password = '' ORDER BY comment_date_gmt DESC LIMIT 10";
$comments = $wpdb->get_results($sql);
foreach ($comments as $comment) {
$output .= "\n<li>".get_avatar(get_comment_author_email(), 32)."<a href=\"" . get_permalink($comment->ID) ."#comment-" . $comment->comment_ID . "\" title=\"on " .$comment->post_title . "\">" . strip_tags($comment->com_excerpt)."</a>...</li>";
}
$output = convert_smilies($output);
echo $output;
?>
</ul>

说明:

  • comment_date_gmt DESC LIMIT 10 中的 10 是指要显示的评论个数
  • SUBSTRING(comment_content,1,18) 中的 18 是指每条评论的中文文字个数
  • ('comment_author_email'), 32) 中的 32 是指头像的图片大小
  • zwwooooo 改为你自己的用户名(就是不显示自己的评论)
  • 加了个 class “recentcomments”,可以在css里面定义样式

如果你使用了 Willin 的头像缓存代码,那么可以修改如下:

<h2>Recent Comments</h2>
<ul class="recentcomments">
<?php
global $wpdb;
$sql = "SELECT DISTINCT ID, post_title, post_password, comment_ID, comment_post_ID, comment_author, comment_date_gmt, comment_approved, comment_type,comment_author_url,comment_author_email, SUBSTRING(comment_content,1,18) 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 != 'zwwooooo' AND post_password = '' ORDER BY comment_date_gmt DESC LIMIT 10";
$comments = $wpdb->get_results($sql);
foreach ($comments as $comment) {
$a= get_bloginfo('wpurl') .'/avatar/'.md5(strtolower($comment->comment_author_email)).'.jpg';
$output .= "\n<li><img src='". $a ."' alt='' title='".$comment->comment_author."' class='avatar' /><a href=\"" . get_permalink($comment->ID) ."#comment-" . $comment->comment_ID . "\" title=\"on " .$comment->post_title . "\">" . strip_tags($comment->com_excerpt)."</a>...</li>";
}
$output = convert_smilies($output);
echo $output;
?>
</ul>

说明:

  • comment_date_gmt DESC LIMIT 10 中的 10 是指要显示的评论个数
  • SUBSTRING(comment_content,1,18) 中的 18 是指每条评论的中文文字个数
  • 头像的图片大小自行在style.css增加个名为“recentcomments”的 class 定义(例如width:32px; height:32px;)
  • zwwooooo 改为你自己的用户名(就是不显示自己的评论)
  • 参数 $a 是缓存路径

不要迷恋分割线,分割线只是个传说

现在是文章的重点,为了能显示带链接开头的评论,我找了很多插件,都是不能显示带连接开头的评论内容,后来突然想到国产的 WP Kit CN 这个插件,一试它可以显示带连接的评论,这下有眉目了。

经过翻看它的代码,发现它是自定义了一个截断函数:function cut_str($string, $sublen, $start = 0, $code = 'UTF-8'),于是发挥 CP 党的精神直接挪到我的主题 function.php 里面,经测试 is OK!小高兴了一下,所以就有了这篇文章了。

下面是继续贴代码:

第一步:把下面的代码加到主题文件的 function.php 里面

function cut_str($string, $sublen, $start = 0, $code = 'UTF-8')
{
 if($code == 'UTF-8')
 {
 $pa = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/";
 preg_match_all($pa, $string, $t_string);
 if(count($t_string[0]) - $start > $sublen) return join('', array_slice($t_string[0], $start, $sublen))."...";
 return join('', array_slice($t_string[0], $start, $sublen));
 }
 else
 {
 $start = $start*2;
 $sublen = $sublen*2;
 $strlen = strlen($string);
 $tmpstr = '';
 for($i=0; $i<$strlen; $i++)
 {
 if($i>=$start && $i<($start+$sublen))
 {
 if(ord(substr($string, $i, 1))>129) $tmpstr.= substr($string, $i, 2);
 else $tmpstr.= substr($string, $i, 1);
 }
 if(ord(substr($string, $i, 1))>129) $i++;
 }
 if(strlen($tmpstr)<$strlen ) $tmpstr.= "...";
 return $tmpstr;
 }
}

第二步:修改过的最新评论代码(调用第一步的截断函数)

<h2>Recent Comments</h2>
<ul class="recentcomments">
<?php
global $wpdb;
$sql = "SELECT DISTINCT ID, post_title, post_password, comment_ID, comment_post_ID, comment_author, comment_date_gmt, comment_approved, comment_type,comment_author_url,comment_author_email, comment_content 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 != 'zwwooooo' AND post_password = '' ORDER BY comment_date_gmt DESC LIMIT 12";
$comments = $wpdb->get_results($sql);
foreach ($comments as $comment) {
$output .= "\n<li>".get_avatar(get_comment_author_email(), 32)."<a href=\"" . get_permalink($comment->ID) ."#comment-" . $comment->comment_ID . "\" title=\"on " .$comment->post_title . "\">" . cut_str(strip_tags($comment->com_excerpt),18)."</a></li>";
}
$output = convert_smilies($output);
echo $output;
?>
</ul>

说明:

  • comment_date_gmt DESC LIMIT 12 中的 12 是要显示的评论数量
  • cut_str(strip_tags($comment->com_excerpt),18) 中的 18 是每条评论要显示的文字数量
  • zwwooooo 是用户名,目的是不显示此用户的评论,改为自己的用户名吧
  • recentcomments 为自定义 class
  • get_avatar(get_comment_author_email('comment_author_email'), 32) 中的 32 是头像大小

如果你使用 Willin 的头像缓存代码,那么代码如下:

<h2>Recent Comments</h2>
<ul class="recentcomments">
<?php
global $wpdb;
$sql = "SELECT DISTINCT ID, post_title, post_password, comment_ID, comment_post_ID, comment_author, comment_date_gmt, comment_approved, comment_type,comment_author_url,comment_author_email, comment_content 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 != 'zwwooooo' AND post_password = '' ORDER BY comment_date_gmt DESC LIMIT 12";
$comments = $wpdb->get_results($sql);
foreach ($comments as $comment) {
$a= get_bloginfo('wpurl') .'/avatar/'.md5(strtolower($comment->comment_author_email)).'.jpg';
$output .= "\n<li><img src='". $a ."' alt='' title='".$comment->comment_author."' class='avatar' /><a href=\"" . get_permalink($comment->ID) ."#comment-" . $comment->comment_ID . "\" title=\"on " .$comment->post_title . "\">" . cut_str(strip_tags($comment->com_excerpt),18)."</a></li>";
}
$output = convert_smilies($output);
echo $output;
?>
</ul>

OK,折腾完,又无聊去了 = =

延伸折腾:不是bug的解决方法《带头像显示的最新评论代码 - 链接 Bug 修正

声明: 除非注明,ZWWoOoOo文章均为原创,转载请以链接形式标明本文地址
本文地址: http://zww.me/archives/24736

Filed under

WP Trick

| Tags:

,

Related Posts

Most Popular

151 Comments.

⊕Leave a comment?
  1. 郑永 郑永 Firefox 3.6Windows XP

    seo搞的很不错啊,一搜索就又到你网站来了 :) 内容很优秀。

    41楼
    • zwwooooo zwwooooo Firefox 3.6.2Windows 7

      @郑永 呃~我写文章从来没注意过seo问题,或许我有几篇文章是写这个代码的吧

  2. 郑永 郑永 Firefox 3.6Windows XP

    再次请教一下,为什么侧边输出评论,不能屏蔽自己的评论呢?貌似因为读者墙已经调用了,所以下面就无法执行呢?

    42楼
    • zwwooooo zwwooooo Firefox 3.6.2Windows 7

      @郑永 你没仔细看文章?找到

      AND comment_author != 'zwwooooo'

      这里面的zwwooooo就是博主自己的用户名,你改为你的就行了

  3. Z theme » Blog Archive » zww Line - pingback on 2010/05/25/ 21:37
    44楼
  4. 阿邙 阿邙 Internet Explorer 8.0 (Compatibility Mode)Windows 7

    几天谷歌更新了方向链接貌似,
    32个20几个都是你这里过去的..

    可恶的是,给俺pr弄成0了..

    45楼
  5. StartOver - trackback on 2010/06/06/ 14:05
    46楼
  6. 阿邙 阿邙 ChromePlus 1.3.9.0Windows 7

    有很多人用了你提供的很迷茫啊
    该更新一下了
    显示的头像都是默认的谜样人物
    http://kan.willin.org/?p=1278&cpage=1#comment-4436
    这里willin给出了答案。。

    47楼
  7. 48楼
  8. 洁 Google Chrome 6.0.422.0Windows XP

    看来还得来这里找,终于找到答案了

    49楼
  9. wei wei Firefox 3.6.6GNU/Linux x64

    你好,我用了这段代码,可以显示评论,但没有评论的用户名,只有评论的内容,请问哪里可以改吗?谢谢!

    50楼
    • zwwooooo zwwooooo Firefox 3.6.6Windows 7

      @wei

      $output .= "\n<li>".get_avatar(get_comment_author_email(), 32)."<a href=\"" . get_permalink($comment->ID) ."#comment-" . $comment->comment_ID . "\" title=\"on " .$comment->post_title . "\">" . cut_str(strip_tags($comment->com_excerpt),18)."</a></li>";
      改为:
      $output .= "\n<li>".get_avatar(get_comment_author_email(), 32). $comment->comment_author .":<br/><a href=\"" . get_permalink($comment->ID) ."#comment-" . $comment->comment_ID . "\" title=\"on " .$comment->post_title . "\">" . cut_str(strip_tags($comment->com_excerpt),18)."</a></li>";
      注意:这里面的<br/>是换行,可能需要更改css,如果你不想换行,就把<br/>去掉

    • wei wei Firefox 3.6.6GNU/Linux x64

      @zwwooooo 谢谢!
      还有问一下博主这里的评论样式怎么实现啊?我看了你这里的文章,试了一下之后发现只开两层嵌套评论,子评论中的reply按钮就没了。。。

    • zwwooooo zwwooooo Firefox 3.6.6Windows 7

      @wei wp的设定本来就是这样,我这用jQuery改了,去jinwen那里,他写过,但不一定适合你。

  10. 主题Squash | 采薇 - pingback on 2011/01/29/ 22:11
    51楼
  11. 阿溪 阿溪 Internet Explorer 8.0Windows 7

    OK啦!!
    终于!!
    感谢zwwooooooooooooo大锅啊!

    52楼
  12. Wayne Wayne Firefox 4.0.1Windows 7

    学习。另外请问博主用的代码插件是那个呢?风格挺好。

    54楼
  13. WordPress Theme:zww Line (beta 2) | xperience - pingback on 2011/06/27/ 15:05
    55楼
  14. 延迪 延迪 Firefox 4.0.1Windows XP

    我用了代码,没反应!不知是什么问题?

    56楼
  15. 57楼
  16. toptpe toptpe Comodo Dragon 12.2.0.0Windows 7

    大侠,怎么让鼠标放到评论者头像上就可以显示评论者的名称啊?

    58楼
  17. 测试下,如果我的名字很长,那么侧边评论是怎么显示的!

    59楼

Leave a Reply


Trackbacks and Pingbacks: