根据@Rarst的回答,我创建了以下解决方法。
附加字段created_on
应在创建时添加到用户的元数据。这是为了识别创建用户的子域以及配置文件映像可能驻留的位置。因此,请将其添加到您的代码中;)
将此函数放置在一个文件中,从中可以获取用户数据或functions.php
你的主题。
/**
* Gets the profile image of a customer or regular user (editor/participant)
* @param $user_id - id of the user to get the image for
* @param $image_name - name of the image-size to return
* @return array|bool - returns image source data, or false
*/
function getUserProfileImageSrc( $user_id, $image_name ){
$profile_image_id = get_user_meta( $user_id, \'prof-pic\', true ); //image of profile picture id
$created_on = get_user_meta( $user_id, \'created_on\', true ); //id of blog where the user was created
$created_here = ($created_on == get_current_blog_id() ) ? true : false; //check if the user was created on the current site
$image_id = $profile_image_id ? $profile_image_id : false; //check if $profile_image_id exists, otherwise false
if( $created_here ){
return wp_get_attachment_image_src( $image_id, $image_name ); //Image created on current site, get & return image
} elseif( !$created_here ){
$current_blog = get_current_blog_id(); //get current blog id
switch_to_blog( $created_on ); //switch to blog to query correct tables
$img = wp_get_attachment_image_src( $image_id, $image_name ); //get image
switch_to_blog( $current_blog ); //switch back to current blog
return $img;
}
return false; //something didn\'t work, return false
}
Addition: 一块地,
created_on
, 需要使用创建用户的博客的id进行添加。像这样:
//creating user
//lots of fancyness
update_user_meta( $new_user_id, \'created_on\', get_current_blog_id() );
//finish creating user
所以。希望这能在将来帮助别人。再次感谢@Rarst指出,图像以WP帖子类型上传,因此将其绑定到博客/子域,而不是ID!