Conditional avatar

时间:2013-09-28 作者:user2591811

我正在使用一个插件,允许用户上传个人资料图形来代替头像。插件为“User Avatar“。

我使用以下代码显示用户配置文件图形:

    echo get_avatar( get_the_author_meta(\'user_email\'), $size = \'60\')
它工作正常,但我希望它能够正常工作,这样,如果用户没有用户配置文件图形,它默认不显示任何内容。如果我在没有用户头像的情况下使用头像讨论设置显示“空白”,我仍然会得到环绕在图形所在位置的文本,从而在用户配置文件所在位置创建一个空白方块。

有人有什么建议吗?

提前感谢您的考虑。

2 个回复
SO网友:Brooke.

几个月前,我在WordPress上写了一篇讨论。关于此的组织线程。请查看:http://wordpress.org/support/topic/custom-avatar-4?replies=5

以下是您想要的头像模板中需要的代码:

   <?php
    $uploads = wp_upload_dir(); //WordPress upload path
    $uploads_url = ( $uploads[\'baseurl\'] ); //full url of upload dir
    $uploads_dir = ( $uploads[\'basedir\'] ); //full path of upload dir
    $user_id = $post->post_author; //id of author (if used in loop)
    $avatar_filename = user_avatar_avatar_exists($user_id ); //function from plugin

if (file_exists($uploads_dir.\'/avatars/\'.$user_id .\'/\'.$avatar_filename)) {
    echo (\'<img src=\'.$uploads_dir.\'/avatars/\'.$user_id .\'/\'.$avatar_filename.\' class="user-avatar"/>\');
}
else {
    echo get_avatar( get_the_author_meta(\'user_email\'), $size = \'60\');
} ?>
编辑:若要在gravar为空时不显示任何内容,请使用此选项(线程上方的源):

   <?php
    $uploads = wp_upload_dir(); //WordPress upload path
    $uploads_url = ( $uploads[\'baseurl\'] ); //full url of upload dir
    $uploads_dir = ( $uploads[\'basedir\'] ); //full path of upload dir
    $user_id = $post->post_author; //id of author (if used in loop)
    $avatar_filename = user_avatar_avatar_exists($user_id ); //function from plugin

   $hash = md5(strtolower(trim($user->user_email)));
   $uri = \'http://www.gravatar.com/avatar/\' . $hash . \'?d=404\';
   $headers = @get_headers($uri);


if (((preg_match("|200|", $headers[0])) || (file_exists($uploads_dir.\'/avatars/\'.$user_id .\'/\'.$avatar_filename))) {

if (file_exists($uploads_dir.\'/avatars/\'.$user_id .\'/\'.$avatar_filename)) {
    echo (\'<img src=\'.$uploads_dir.\'/avatars/\'.$user_id .\'/\'.$avatar_filename.\' class="user-avatar"/>\');
}
else {
    echo get_avatar( get_the_author_meta(\'user_email\'), $size = \'60\');
} 

} ?>

SO网友:Banago

以下是实现此功能的过滤器:

function ns_filter_avatar($avatar, $id_or_email, $size, $default, $alt, $args) 
{
    $headers = @get_headers( $args[\'url\'] );
    if( ! preg_match("|200|", $headers[0] ) ) {
        return;
    }
    return $avatar; 
}
add_filter(\'get_avatar\',\'ns_filter_avatar\', 10, 6);
要工作,你必须增加价值404 作为第三个参数$defaultget_avatar(). 示例:

get_avatar( $user_email, 45, \'404\' )

结束

相关推荐

Snippet for own (gr)avatars

使用了以下代码段(效果很好),但生成了重定向链:(。If (function_excists(\'basicgravatar\') function basicgravatar ($avatar_defaults) { $myavatar = get_stylesheet_directory_uri() . \'/images/gravatar.png\'; //Child theme //$myavatar = get_bloginfo(\'templa