我正在开发一个网站,用户可以在其中捕获他/她的图片(通过.getusermedia),该图片已更新为帖子缩略图。
问题是所有用户都会更新帖子缩略图 - 我希望仅为该特定用户更新帖子缩略图
function Generate_Featured_Image( $filename, $parent_post_id ){
require('/wp-load.php');
$filetype = wp_check_filetype( basename( $filename ), null );
$wp_upload_dir = wp_upload_dir();
$attachment = array(
'guid' => $wp_upload_dir['url'] . '/' . basename( $filename ),
'post_mime_type' => $filetype['type'],
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment, $filename, $parent_post_id );
require_once( ABSPATH . 'wp-admin/includes/image.php' );
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
wp_update_attachment_metadata( $attach_id, $attach_data );
//add_user_meta( $current_user_id, $parent_post_id, $attach_id);
set_post_thumbnail( $parent_post_id, $attach_id );
}
的script.php
Generate_Featured_Image( $addroot.$current_user_id.$extimage, 88 );
// addroot=path ext-extension(.jpg) (this is name of file saved)
我尝试使用add_user_meta来完成任务,但甚至无法启动
更新
<?php
// $filename is succesfully saved as currentuserid+.jpg.
$addroot = '/wp-content/uploads/2016/09/';
$current_user_id = get_current_user_id();
$extimage = '.jpg';
$filename = $addroot.$current_user_id.$extimage;
$filetype = wp_check_filetype( basename( $filename ), null );
// Get the path to the upload directory.
$wp_upload_dir = wp_upload_dir();
// Prepare an array of post data for the attachment.
$attachment = array(
'guid' => $wp_upload_dir['url'] . '/' . basename( $filename ),
'post_mime_type' => $filetype['type'],
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
'post_content' => '',
'post_status' => 'inherit'
);
// Insert the attachment.
$attach_id = wp_insert_attachment( $attachment, $filename, 0 );
require_once( ABSPATH . 'wp-admin/includes/image.php' );
// Generate the metadata for the attachment, and update the database record.
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
wp_update_attachment_metadata( $attach_id, $attach_data );
update_user_meta( get_the_ID(), $filename , $_POST[ $filename ] );
?>
如何进一步通过usermeta调用$ attach_id图像并将其显示为用户缩略图 - 无法计算此步骤
答案 0 :(得分:1)
您无法根据用户为帖子设置不同的精选图片。只有一个帖子,因此只有一个特色图片
如果要根据查看帖子的用户显示不同的图像,请使用wp_insert_attachment( $attachment, $filename, 0)
保存以插入附件而不将其绑定到帖子。然后将$attach_id
保存到usermeta
表。然后,当用户正在查看该帖子时,只需使用$attach_id
获取get_user_meta
并显示该图片(例如,您可以使用wp_get_attachment_url
),而不是帖子的精选图片。
<强>更新强>
首先,保存用户元应该如下所示
update_user_meta($current_user_id, '_avatar_id', $attach_id);
其次,在开始时,您应该检查用户是否使用is_user_logged_in
功能登录。
第三,您应该检查用户之前是否有头像并将其删除(我的意思是,为什么在他们有一个新的之后存储旧的,对吧?)如下:
$old_attach=get_user_meta($current_user_id, '_avatar_id', true);
if(is_numeric($old_attach))
{
wp_delete_attachment($old_attach, true);
}
保存头像的最终代码应如下所示:
if (is_user_logged_in())
{
$addroot = '/wp-content/uploads/2016/09/';
$current_user_id = get_current_user_id();
$extimage = '.jpg';
$filename = $addroot . $current_user_id . $extimage;
$filetype = wp_check_filetype(basename($filename), null);
$wp_upload_dir = wp_upload_dir();
$attachment = array(
'guid' => $wp_upload_dir['url'] . '/' . basename($filename),
'post_mime_type' => $filetype['type'],
'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment($attachment, $filename, 0);
require_once( ABSPATH . 'wp-admin/includes/image.php' );
wp_update_attachment_metadata($attach_id, wp_generate_attachment_metadata($attach_id, $filename));
$old_attach = get_user_meta($current_user_id, '_avatar_id', true);
if (is_numeric($old_attach))
{
wp_delete_attachment($old_attach, true);
}
update_user_meta($current_user_id, '_avatar_id', $attach_id);
}
else
{
//show error here or something
}
现在,要访问它,您可以编写一个简单的函数,如下所示:
function get_user_avatar()
{
if (is_user_logged_in())
{
$avatar_id = get_user_meta(get_current_user_id(), '_avatar_id', true);
if (is_numeric($avatar_id))
{
return'<img src="' . wp_get_attachment_url($avatar_id) . '" alt="User avatar"/>';
}
else
{
return '<img src="url_to_your_default_avatar" alt="User avatar"/>';
}
}
return false;
}