willin简单头像缓存函数my_avatar()——ZWWoOoOo修改版

转过来主要是因为太强大了,留着自己以后慢慢用。。。。。。。

既然boss们都把代码写成函数了,我就直接CP函数的了,请看:

willin缓存的方法

先在你的網站 wp-content 的同級目錄建立資料夾: /avatar 權限:755, 這是準備 gravatar 緩存的路徑.
準備一張適合你模板尺寸的默認頭像, 名為”default.jpg” 放在此路徑.

把下面的代码添加的你的functions.PHP文件中(ZWWoOoOo修改,原文请看http://zww.me/archives/25296)

/* Mini Gavatar Cache by Willin Kan. */
function my_avatar( $email, $size = '42', $default = '', $alt = '' ) {
// $alt = (false === $alt) ? '' : esc_attr( $alt );
$alt = ('' == $alt) ? '' : $alt ;
$f = md5( strtolower( $email ) );
$a = get_bloginfo('wpurl'). '/avatar/'. $f. '.jpg';
$e = ABSPATH. 'avatar/'. $f. '.jpg';
$t = 1209600; //??14?, ??:?
if ( empty($default) ) $default = get_bloginfo('template_directory'). '/img/default.jpg';
if ( !is_file($e) || (time() - filemtime($e)) > $t ){ //当文件不存在或缓存超过14天才更新
$r = get_option('avatar_rating');
//$g = sprintf( "http://%d.gravatar.com", ( hexdec( $f{0} ) % 2 ) ). '/avatar/'. $f. '?s='. $size. '&d='. $default. '&r='. $r; // wp 3.0的服务器
$g = 'http://www.gravatar.com/avatar/'. $f. '?s='. $size. '&d='. $default. '&r='. $r; //旧服务器
copy($g, $e); $a = esc_attr($g);
}
if (filesize($e) < 500) copy($default, $e);
$avatar = "<img title='{$alt}' alt='{$alt}' src='{$a}' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' />";
return apply_filters('my_avatar', $avatar, $email, $size, $default, $alt);
}
// -- END ----------------------------------------

使用方法:

my_avatar( $email, $size = '40', $default = '', $alt = '' )

$email: 评论者email
$size: 头像图片大小
$default: 默认头像位置,如果你不设置,那么在 avatar 缓存目录放一个 default.jpg 作为默认头像。
$alt: 评论者昵称

举例

1. 文章评论页面调用一般是这样:

<?php echo my_avatar($comment->comment_author_email,$size='40',$default='',$comment->
comment_author); ?>

不需要定义$default和$alt,下面这样就可以了

<?php echo my_avatar($comment->comment_author_email,$size='40'); ?>

2. 最新评论代码里是这样的:

my_avatar($rc_comm->comment_author_email,$size='40',$default='',$rc_comm->comment_author)

附zww的最新评论代码带头像缓存的

<h3>Recent Comments</h3>
<ul class="recentcomments">
<?php //2010/4/25 更新by willin
$limit_num = '8'; //这里定义评论的数量
$my_email = "'" . get_bloginfo ('admin_email') . "'"; //检测博主的邮箱,博主的不显示
$rc_comms = $wpdb->get_results("
SELECT ID, post_title, comment_ID, comment_author, comment_author_email, comment_content
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 comment_author_email != $my_email
ORDER BY comment_date_gmt
DESC LIMIT $limit_num
");
$rc_comments = '';
foreach ($rc_comms as $rc_comm) { //get_avatar($rc_comm,$size='50')
$rc_comments .= "<li>" . my_avatar($rc_comm->comment_author_email,$size='40')
. "<span class='zsnos_comment_author'>".$rc_comm->comment_author.": </span><a href='"
. get_permalink($rc_comm->ID) . "#comment-" . $rc_comm->comment_ID
//. htmlspecialchars(get_comment_link( $rc_comm->comment_ID, array('type' => 'comment'))) // 可取代上一行,实现评论分页,但耗资源
. "' title='on " . $rc_comm->post_title . "'>" . strip_tags($rc_comm->comment_content)
. "</a></li>\n";
}
$rc_comments = convert_smilies($rc_comments);
echo $rc_comments;
?>
</ul>

自己调整参数和css之后慢慢欣赏自己的大作吧。。。。

资料整理自zww和willin,有需要的请移步

这里是自动检测博主的邮件,实现博主的评论不显示

willin简单头像缓存函数my_avatar()——ZWWoOoOo修改版》上有 3 条评论

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

*

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>