终于完美解决WordPress头像问题
昨天还在纠结着关于如何解决WordPress头像的后遗症问题,经过了一番努力与求助了 Google , 终于算是完美的解决了这个问题.终于写成将代码写进Function,只是在需要用到的地方,用函数名来调用.以后修改就相对方便多了,好了,废话不多说,看看代码是怎么写的.
其实,这个代码是Willin 的 新版 Gravatar 緩存 ,下面是代码,直接复制进function即可.
/*头像缓存*/
function my_avatar( $email, $size = '42', $default = '', $alt = '' ) {
$alt = esc_attr( $alt );
$f = md5( strtolower( $email ) );
$w = get_bloginfo('wpurl'); // 如果想放在 wp-content 路徑之下, 改為 $w = WP_CONTENT_URL;
$a = $w. '/avatar/'. $f. '.jpg';
$e = ABSPATH. 'avatar/'. $f. '.jpg'; // 如果想放在 wp-content 路徑之下, 改為 $e = WP_CONTENT_DIR. '/avatar/'. $f. '.jpg';
$t = 1209600; //設定14天, 單位:秒
if ( empty($default) ) $default = $w. '/avatar/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); //新頭像 copy 時, 取 gravatar 顯示
}
if (filesize($e) < 500) copy($default, $e);
echo "<img title='{$alt}' alt='{$alt}' src='/index/thumb.png' data-original='{$a}' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' />";
}
function my_avatar( $email, $size = '42', $default = '', $alt = '' ) {
$alt = esc_attr( $alt );
$f = md5( strtolower( $email ) );
$w = get_bloginfo('wpurl'); // 如果想放在 wp-content 路徑之下, 改為 $w = WP_CONTENT_URL;
$a = $w. '/avatar/'. $f. '.jpg';
$e = ABSPATH. 'avatar/'. $f. '.jpg'; // 如果想放在 wp-content 路徑之下, 改為 $e = WP_CONTENT_DIR. '/avatar/'. $f. '.jpg';
$t = 1209600; //設定14天, 單位:秒
if ( empty($default) ) $default = $w. '/avatar/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); //新頭像 copy 時, 取 gravatar 顯示
}
if (filesize($e) < 500) copy($default, $e);
echo "<img title='{$alt}' alt='{$alt}' src='/index/thumb.png' data-original='{$a}' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' />";
}
其中的路径问题,我用了a.itlobo.com来存放,其实头像也还是在博客主机上的,就是用了另外一个二级域名而已.
关于评论区的调用,我是这样子修改的,其实,也是在主题的function里面,我用的主题是Inove.
<div class="pic">
<?php if (function_exists('my_avatar') && get_option('show_avatars')) { echo my_avatar($comment->comment_author_email, $size = '32' ,$default = 'http://a.itlobo.com/avatar/default.jpg'); } ?>
</div>
<?php if (function_exists('my_avatar') && get_option('show_avatars')) { echo my_avatar($comment->comment_author_email, $size = '32' ,$default = 'http://a.itlobo.com/avatar/default.jpg'); } ?>
</div>
侧边栏的最新评论的代码修改为如下,在sidebar.php文件:
<h3>最新评论</h3>
<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->commentsLEFT OUTER JOIN$wpdb->postsON ($wpdb->comments.comment_post_ID =$wpdb->posts.ID) WHERE comment_approved = '1' AND comment_type = '' AND comment_author != 'itlobo' AND post_password = '' ORDER BY comment_date_gmt DESC LIMIT 10";
$comments = $wpdb->get_results($sql);
foreach ($comments as $comment) {
?>
<li><?php echo my_avatar( $comment->comment_author_email, $size='', $default='http://a.itlobo.com/avatar/default.jpg', $alt=strip_tags($comment->comment_author) ); ?><a href="<?php echo get_permalink($comment->ID); ?>#comment-<?php echo $comment->comment_ID; ?>" title="<?php echo $comment->post_title; ?>上的评论"><?php echo cut_str(strip_tags($comment->com_excerpt),18); ?></a></li>
<?php } ?>
</ul>
<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->commentsLEFT OUTER JOIN$wpdb->postsON ($wpdb->comments.comment_post_ID =$wpdb->posts.ID) WHERE comment_approved = '1' AND comment_type = '' AND comment_author != 'itlobo' AND post_password = '' ORDER BY comment_date_gmt DESC LIMIT 10";
$comments = $wpdb->get_results($sql);
foreach ($comments as $comment) {
?>
<li><?php echo my_avatar( $comment->comment_author_email, $size='', $default='http://a.itlobo.com/avatar/default.jpg', $alt=strip_tags($comment->comment_author) ); ?><a href="<?php echo get_permalink($comment->ID); ?>#comment-<?php echo $comment->comment_ID; ?>" title="<?php echo $comment->post_title; ?>上的评论"><?php echo cut_str(strip_tags($comment->com_excerpt),18); ?></a></li>
<?php } ?>
</ul>
使用上面的代码注意换为您的用户名哦....
好了,现在头像缓存代码已经都写进function了,也达到了我的目的啦.OK,就此收工!
赏
转载本站原创文章请注明:文章转自 挨踢路,链接: https://888929.tm7j5.group/articles/1435.html
我的博客头像是有了,只是没有出现回复是的层次感啊,如何弄啊?