如何获取作者的Gravatar网址? [英] How to get author's Gravatar url?

查看:70
本文介绍了如何获取作者的Gravatar网址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望作者的图片出现在single.php页面的某个部分,并且我使用了在wordpress堆栈交换中找到的这段代码来获取作者Gravatar的URL,以便将图片用作背景DIV的图片:

I want the author's pic to appear in some part of the single.php page, and I used this code I found somewhere on the wordpress stack exchange to get the URL of the author Gravatar in order to use the pic as a background image of a DIV :

functions.php中的这段代码:

This code in functions.php :

if ( function_exists('register_sidebar') )
    register_sidebar();
add_theme_support( 'post-thumbnails' );
add_image_size( 'related', 300 , 200 , array(bottom, right) ); 
function get_avatar_url($get_avatar){
    preg_match("/src='(.*?)'/i", $get_avatar, $matches);
    return $matches[1]; };

这在single.php中:

And this in the single.php :

<div style="background:url('<?php echo get_avatar_url(get_avatar( $curauth->ID, 150 )); ?>');"> </div>

奇怪的是,尽管Gravatar已链接到作者帐户,并且显示在管理面板上,但它返回默认的神秘人"化身而不是作者的Gravatar!

The strange thing about it is that it returns the default "mystery man" avatar instead of the author's Gravatar, although the Gravatar is linked to the author account and it appears on the admin panel !

我该如何解决这个问题?

How can I solve this problem ?

推荐答案

此函数将为您提供GRAVATAR URL!

This function will simply give you GRAVATAR URL!

感谢Musik8101,我根据您的代码提出了自定义解决方案.

Thanks Musik8101, I came up with my custom solution based on your code.

就在这里,并且运作良好:

Here it is and it's working good:

 function my_gravatar_url() { // Get user email
$user_email = get_the_author_meta( 'user_email' );
// Convert email into md5 hash and set image size to 80 px
$user_gravatar_url = 'http://www.gravatar.com/avatar/' . md5($user_email) . '?s=80';
echo $user_gravatar_url; } 

在模板中只需使用:

<?php my_gravatar_url() ?>

注意:它必须在循环内使用. 对于在get_avatar_url方法上遇到问题的人,可以在Wordpress 4.2上使用.

Notice: it must be used inside a loop. Works on Wordpress 4.2 for those who had problems with get_avatar_url method.

这篇关于如何获取作者的Gravatar网址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆