個人網站CMS主題TStyl的Gravatar頭像緩存方法
來源:程序員人生 發布時間:2013-11-24 02:56:21 閱讀次數:3181次
首先在網站的根目錄建立文件夾:avatar,權限為:755, 這是準備 gravatar 緩存的路徑。
然后把一個50×50的jpg圖片改名為:default.jpg,把它存到剛才建立的 gravatar 緩存文件夾 avatar 里面。
下面是修改代碼的方法:
1,進入后臺,在 外觀 編輯 那里,然后編輯 functions.php 文件,也可以把這個文件下到本地電腦再進行編輯。
在functions.php文件找到下面的代碼
foreach ($comments as $comment) {
$email=$comment->comment_author_email;
在上面這兩行代碼中間插入下面的代碼進去:
(也就是在 foreach ($comments as $comment) { 下面添加下面的代碼進去)
$p = ‘avatar/’;
$f = md5(strtolower($comment->comment_author_email));
$a = $p . $f .’.jpg’;
$e = ABSPATH . $a;
if (!is_file($e)){ //當頭像不存在就更新
$d = get_bloginfo(‘wpurl’). ‘/avatar/default.jpg’;
$s = ’32′; //頭像大小 自行根據自己模板設置
$r = get_option(‘avatar_rating’);
$g = ‘http://www.gravatar.com/avatar/’.$f.’.jpg?s=’.$s.’&d=’.$d.’&r=’.$r;
copy($g, $e);
if ( filesize($e) == 0 ){ copy($d, $e); }
};
2,找到下面的代碼
$email=$comment->comment_author_email;
$grav_url=”http://www.gravatar.com/avatar/”.md5($email).”?s=32″;
把 $grav_url=”http://www.gravatar.com/avatar/”.md5($email).”?s=32″; 改成 $grav_url=”../$a”;
到這時已經有一部分頭像實現了頭像緩存了。你可以先保存測試下效果,再打開網站,看看網站右邊的 最新評論 和 最新留言 的頭像圖片地址是不是變成自己網站的址地了。
3,繼續編輯 functions.php 文件,找到下面的內容:
//最新評論、留言
function recent_comment
在 //最新評論、留言 上面插入下面的代碼:
function mytheme_comment($comment, $args, $depth) {
$GLOBALS['comment'] = $comment; ?>
<li <?php comment_class(); ?> id=”li-comment-<?php comment_ID() ?>”>
<div id=”comment-<?php comment_ID(); ?>”>
<div>
<?php
$p = ‘avatar/’;
$f = md5(strtolower($comment->comment_author_email));
$a = $p . $f .’.jpg’;
$e = ABSPATH . $a;
if (!is_file($e)){ //當頭像不存在就更新
$d = get_bloginfo(‘wpurl’). ‘/avatar/default.jpg’;
$s = ’48′; //頭像大小 自行根據自己模板設置
$r = get_option(‘avatar_rating’);
$g = ‘http://www.gravatar.com/avatar/’.$f.’.jpg?s=’.$s.’&d=’.$d.’&r=’.$r;
copy($g, $e);
if ( filesize($e) == 0 ){ copy($d, $e); }
};
?>
<img src=’<?php bloginfo(‘wpurl’); ?>/<?php echo $a ?>’ alt=” class=’avatar’ />
<?php printf(__(‘<cite>%s</cite> <span>says:</span>’), get_comment_author_link()) ?>
</div>
<?php if ($comment->comment_approved == ’0′) : ?>
<em><?php _e(‘Your comment is awaiting moderation.’) ?></em>
<br />
<?php endif; ?>
<div><a href=”<?php echo htmlspecialchars( get_comment_link( $comment->comment_ID ) ) ?>”><?php printf(__(‘%1$s at %2$s’), get_comment_date(), get_comment_time()) ?></a><?php edit_comment_link(__(‘(Edit)’),’ ‘,”) ?></div>
<?php comment_text() ?>
<div>
<?php comment_reply_link(array_merge( $args, array(‘depth’ => $depth, ‘max_depth’ => $args['max_depth']))) ?>
</div>
</div>
<?php
}
更新 functions.php 文件。
4,編輯 comments.php 文件,找到 <?php wp_list_comments(‘avatar_size=48′); ?> ,把這段代碼替換為下面的代碼:
<?php wp_list_comments(‘type=comment&callback=mytheme_comment’); ?>
更新 comments.php 文件,完畢。
現在已經全面實現了頭像緩存的功能,不用插件的,綠色環保。此方法不支持后臺頭像緩存。
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈