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

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

Dec 11th, 2009 13:04 | Leave a comment?(155) 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

155 Comments.

⊕Leave a comment?
  1. socekin socekin Google Chrome 4.0.249.25Windows 7

    收藏了,不愧是z大 :wink:

    21楼
  2. 雨人 雨人 Internet Explorer 8.0Windows 7

    嗯,学习了,收藏备用

    22楼
  3. 疾风 疾风 Firefox 3.5.5Windows XP

    你代码真牛 呵呵~

    23楼
  4. muxiaku muxiaku Internet Explorer 7.0Windows XP

    哈哈,看你文章,我点你广告,生活真美妙 :lol:

    24楼
  5. Vicia Vicia Opera 10.10Windows XP

    我用的是中间的那段代码,不带头像缓存的。但是出现的头像全都是同一个默认头像。@_@

    25楼
    • zwwooooo zwwooooo Firefox 3.5.5Windows XP

      @Vicia 是不是哪里搞错了?我测试时没问题啊

    • Vicia Vicia Opera 10.10Windows XP

      @zwwooooo 因为@回复的问题,我又用了你的最后的那个代码。其他都搞定了,但是头像还是全部都一个模样。
      调用头像是不是就是那句“.get_avatar($count->comment_author_email,32).”?在“最活跃朋友”里面,头像调用就很正常。但是评论这个就是不行。@_@

    • zwwooooo zwwooooo Firefox 3.5.5Windows XP

      @Vicia 嗯,“.get_avatar($count->comment_author_email,32).”,如果不行那就郁闷了,我测试时是可以的。问一下,你是直接放在sidebar吧,如果直接将此代码做成function貌似是不能显示

    • Vicia Vicia Opera 10.10Windows XP

      @zwwooooo 是直接放sidebar的,除了那段截断函数。

    • zwwooooo zwwooooo Firefox 3.5.5Windows XP

      @Vicia 那就很奇怪了,能否把你的主题打包发给我?我本地看看。加紧麦田QQ群7874401也可以,如果你用Qq的话

    • Vicia Vicia Opera 10.10Windows XP

      @zwwooooo 已经发到你邮箱了,嘿嘿。多谢啦。 :oops:

    • A.shun A.shun Opera 10.10Windows XP

      [ 小牆判斷這是Spam! ]
      [blockquote]
      缓存的
      [/blockquote]

    • zwwooooo zwwooooo Firefox 3.5.5Windows XP

      @Vicia A.shun说是OP缓存问题,你试试其他浏览器,他还说把get_avatar($count->comment_author_email,32)
      改为
      get_avatar(get_comment_author_email('comment_author_email'), 32)

      你先试试,我再折腾(A.shun的评论给spam了,哇咔咔 :mrgreen:

    • Vicia Vicia Google Chrome 3.0.195.24Windows XP

      @zwwooooo :oops: 谢谢二位,已经成功了。

      听说loading问题也解决了,太好了。 :mrgreen:

    • Vicia Vicia Google Chrome 3.0.195.24Windows XP

      @zwwooooo 我在Opera下无法提交评论,显示“错误:请输入评论。”现在是在chrome下。 :???:

    • A.shun A.shun Opera 10.10Windows XP

      @Vicia
      :sad: test

    • Vicia Vicia Opera 10.10Windows XP

      @A.shun test.

    • Vicia Vicia Opera 10.10Windows XP

      @A.shun test.不好意思,刚刚我Opera抽风了。T_T

    • zwwooooo zwwooooo Firefox 3.5.5Windows XP

      @Vicia 我是实在用不惯OP啊,哈哈

    • zwwooooo zwwooooo Firefox 3.5.5Windows XP

      @A.shun 我在想这层楼会不会成为我博客史上最高呢 :lol:

    • 阿修 阿修 Google Chrome 3.0.195.33Windows 7

      @zwwooooo 加楼……

  6. SATURN SATURN Firefox 3.5.5Windows 7

    发现我已经有点迷糊了

    26楼
  7. andy andy Firefox 3.5.5Windows XP

    代码太长,不看!!!走了

    27楼
  8. luckydog luckydog Internet Explorer 6.0Windows XP

    俺的F2blog也用上了全球通用头像哈

    28楼
  9. 砼砼 砼砼 Internet Explorer 8.0Windows 7

    代码不错,就是代码显示插件难看

    29楼
  10. 碎碎念の部落格 碎碎念の部落格 Internet Explorer 8.0Windows 7

    Z问你个幼稚的问题 :!: 那个分割线的底纹怎么弄的? :arrow:

    30楼
    • zwwooooo zwwooooo Firefox 3.5.5Windows XP

      @碎碎念の部落格 你是说文章里面的?直接写代码啊,或者用菊子曰离线写博软件有直接的按钮。
      如果你要直接写代码,那么打开文章的html源码,找到要加底色的文字

      例如:([ ] 代表 <>)

      [p]我是分割线[/p]

      加底色就这样改:

      [p style="background:#颜色代码"]我是分割线[/p]

    • 碎碎念の部落格 碎碎念の部落格 Maxthon 2.0Windows XP

      @zwwooooo
      (⊙v⊙)嗯 想、知道了 谢谢啦哈哈 :razz:

  11. 锋子 锋子 Maxthon 2.0Windows Vista

    太烦杂了~~有空在弄这~~

    31楼
  12. 32楼
  13. Aaron Aaron Firefox 3.5.7Windows 7

    看了你的文章真实裨益良多啊 :grin:

    33楼
  14. 34楼
  15. bolo bolo Firefox 3.5.7Windows 7

    哈哈,我是把它做成函数的

    35楼
  16. itlobo itlobo Firefox 3.5.7Windows XP

    用了你这里的代码,特来感谢一下.

    36楼
  17. Skyoy Skyoy Firefox 3.6Windows XP

    想用这个,来代替WP- Recent Comments插件。

    可是有点点不能满意哦。能不能加个“下一页”和“上一页”的导航哦,毕竟在右栏显示就10个评论左右,可不方便翻看旧的记录哦。

    37楼
    • zwwooooo zwwooooo Firefox 3.6Windows XP

      @Skyoy 呵呵,也许可以,但没研究,因为如果要多点功能不如直接用 WP- Recent Comments 插件

  18. Landon Landon Firefox 3.5.7Windows XP

    用WP- Recent Comments 解决我一直想解决的问题:)

    38楼
  19. hYle hYle Google Chrome 5.0.335.1Windows XP

    我用的主题不支持小工具,所以借用你的代码 :mrgreen: 还有,头像缓存有必要加吗?刚接触这个 :arrow:

    39楼
  20. 40楼

Leave a Reply


Trackbacks and Pingbacks: