WordPress自带的Gravatar头像加载很慢,这是一个很小白的问题了,不过很多相关的WordPress插件能够结局,虽然使用SSL线路可以加快Gravatar头像访问,但是如果不结合本地缓存,依旧会觉得加载缓慢。
而大多数博主都没有申请Gravatar头像,所以默认的情况下会小时WordPres自带的头像,那头像个人觉得不太美观。
今天本站就分享WordPress纯代码实现用户显示随机头像,让你的WordPress能够自定义用户默认头像。
功能代码
add_filter( 'get_avatar' , 'local_random_avatar' , 1 , 5 );
function local_random_avatar( $avatar, $id_or_email, $size, $default, $alt) {
if ( ! empty( $id_or_email->user_id ) ) {
$avatar = ''.get_template_directory_uri().'/avatar/admin.jpg';
}else{
$random = mt_rand(1, 10);
$avatar = ''.get_template_directory_uri().'/avatar/'. $random .'.jpg';
}
$avatar = "";
return $avatar;
}
使用方法
将以上代码加入到你主题的function.php文件保存即可。
代码解释
其中mt_rand(1, 10);数字为随机图片张数可以自行修改。
图片设置
在当前主题目录中新建一个名称为avatar的文件夹,里面放10张名称连续的jpg图片,比如1.jpg、2.jpg、3.jpg………,和一张用于管理员的图片,名称为admin.jpg
添加上面代码后,只有管理员是显示特定的图片,其他留言者都是随机显示预先准备的头像图片,包括注册用户。
文章来源于互联网:WordPress纯代码实现用户显示随机头像
Hey! I know this is kinda off topic nevertheless I’d figured I’d ask.
Would you be interested in exchanging links or maybe guest writing a blog article
or vice-versa? My blog addresses a lot of the same
subjects as yours and I think we could greatly benefit from each
other. If you might be interested feel free to shoot me an email.
I look forward to hearing from you! Great blog by the
way!
my site – tracfone special
It’s in reality a great and useful piece of info.
I’m satisfied that you shared this helpful info with us.
Please keep us informed like this. Thanks for sharing.
My spouse and I stumbled over here from a different web page
and thought I might check things out. I like what I see so now i am following you.
Look forward to checking out your web page repeatedly.
Attractive component to content. I simply stumbled upon your weblog and in accession capital to claim
that I get actually enjoyed account your blog posts. Any way I’ll be
subscribing in your feeds or even I success you get admission to
constantly quickly.
I’m not that much of a online reader to be honest
but your sites really nice, keep it up! I’ll go ahead and bookmark your website to
come back later on. Cheers
Also visit my web site … coupon
Hi there, I log on to your blogs like every week. Your humoristic style is witty,
keep it up!
Woah! I’m really digging the template/theme of this blog. It’s simple, yet effective.
A lot of times it’s hard to get that “perfect balance” between usability and appearance.
I must say you’ve done a awesome job with this. Additionally, the blog
loads very fast for me on Chrome. Superb Blog!
Aw, this was a really good post. Spending
some time and actual effort to make a top notch article… but what can I say… I hesitate
a whole lot and never manage to get anything done.
Stunning story there. What happened after? Thanks!