修改get_avatar()函数

时间:2018-09-06 08:07:24

标签: php wordpress function avatar gravatar

我找到了一个很棒的教程来创建自定义头像(https://www.billerickson.net/wordpress-custom-avatar/)。基本上,它会检查:

1)如果有自定义头像,请使用它。

2)如果没有自定义头像,请检查是否有头像。如果有图像,请使用它。

3)如果都没有,请显示默认值。

问题在于我已经在wordpress上注册了用户,因此,每当他们发表评论时,它都会使用我为作者设置的自定义头像,用于所有已注册电子邮件的评论者。

这是我苦苦挣扎的代码的一部分:

// If provided an email and it doesn't exist as WP user, return avatar since there can't be a custom avatar
$email = is_object( $id_or_email ) ? $id_or_email->comment_author_email : $id_or_email;
if( is_email( $email ) && ! email_exists( $email ) )
    return $avatar;

$custom_avatar = get_the_author_meta('be_custom_avatar');
if ($custom_avatar) 
    $return = '<img src="'.$custom_avatar.'" width="'.$size.'" height="'.$size.'" alt="'.$alt.'" />';
elseif ($avatar) 
    $return = $avatar;
else 
    $return = '<img src="'.$default.'" width="'.$size.'" height="'.$size.'" alt="'.$alt.'" />';
return $return;

如何进行检查,以确保如果“用户个人资料”中的“ be_custom_avatar”为空,并且没有设置图片,它将返回默认的wordpress $ avatar?

谢谢

1 个答案:

答案 0 :(得分:0)

在WordPress中添加自己的头像

add_filter( 'avatar_defaults', 'custom_avatar' );
    function custom_avatar($avatar_defaults){
    $custom_avatar = get_stylesheet_directory_uri() . '/images/default-avatar.jpg';
    $avatar_defaults[$custom_avatar] = "My Default Avatar";
    return $avatar_defaults;
 }

尝试此代码,您将找到解决方案

相关问题