如何将 thumbnail_size 用于作为 usermeta 上传的用户照片
How do I use thumbnail_size for a user photo which is uploaded as usermeta
我在用户个人资料编辑页面中创建了一个字段,允许用户上传他们的个人资料照片。信息保存在 wp_usermeta
table 下的 user_profile_image
键中。
应用中不会使用Gravatar,用户自己从管理员上传的照片将用作his/her个人资料照片。
我在我的主题中定义了不同的缩略图大小,我可以看到上传的正确缩略图大小。
但我不知道如何应用 thumbnail_size
和 get_the_author_meta 函数,就像我们对 post 缩略图所做的那样。有什么办法吗?
目前我正在这样检索用户的照片:
$profile_picture = get_the_author_meta( 'user_profile_image', $current_author->ID );
但我还需要应用以下缩略图尺寸:
add_image_size( 'blog-author-thumbnail', 450, 675, true); // constrain width to 450
,我不知道怎么办!
如有任何建议,我们将不胜感激。
在开发插件等时,通过 URL 检索图像 ID 很常见。
而且我想你也可以这样做。
// retrieves the attachment ID from the file URL - put this into functions.php
function pippin_get_image_id($image_url) {
global $wpdb;
$attachment = $wpdb->get_col($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid='%s';", $image_url ));
return $attachment[0];
}
// Next part is for displaying the image in your template etc.
// set the image url
// $image_url = 'http://yoursite.com/wp-content/uploads/2011/02/14/image_name.jpg';
// or in your case :
$image_url = $profile_picture;
// store the image ID in a var
$image_id = pippin_get_image_id($image_url);
// retrieve the thumbnail size of our image - you can change the size - so you could use 'blog-author-thumbnail'
$image_thumb = wp_get_attachment_image_src($image_id, 'thumbnail');
// to display the image
echo $image_thumb[0];
我在用户个人资料编辑页面中创建了一个字段,允许用户上传他们的个人资料照片。信息保存在 wp_usermeta
table 下的 user_profile_image
键中。
应用中不会使用Gravatar,用户自己从管理员上传的照片将用作his/her个人资料照片。
我在我的主题中定义了不同的缩略图大小,我可以看到上传的正确缩略图大小。
但我不知道如何应用 thumbnail_size
和 get_the_author_meta 函数,就像我们对 post 缩略图所做的那样。有什么办法吗?
目前我正在这样检索用户的照片:
$profile_picture = get_the_author_meta( 'user_profile_image', $current_author->ID );
但我还需要应用以下缩略图尺寸:
add_image_size( 'blog-author-thumbnail', 450, 675, true); // constrain width to 450
,我不知道怎么办!
如有任何建议,我们将不胜感激。
在开发插件等时,通过 URL 检索图像 ID 很常见。
而且我想你也可以这样做。
// retrieves the attachment ID from the file URL - put this into functions.php
function pippin_get_image_id($image_url) {
global $wpdb;
$attachment = $wpdb->get_col($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid='%s';", $image_url ));
return $attachment[0];
}
// Next part is for displaying the image in your template etc.
// set the image url
// $image_url = 'http://yoursite.com/wp-content/uploads/2011/02/14/image_name.jpg';
// or in your case :
$image_url = $profile_picture;
// store the image ID in a var
$image_id = pippin_get_image_id($image_url);
// retrieve the thumbnail size of our image - you can change the size - so you could use 'blog-author-thumbnail'
$image_thumb = wp_get_attachment_image_src($image_id, 'thumbnail');
// to display the image
echo $image_thumb[0];