当前代码的作用:
I feed post id和;之前上传的图像id,代码处理了所有这些,并完成了下面描述的魔术。结果:unattached old image 和new attached image.
我需要它做什么:
附上same 只需点击几下,即可在媒体屏幕上发布图像。
<小时>Q: 有人知道修复方法吗?或者我在什么地方出错了?欢迎所有评论!
<小时>
//Attach images to post
$gallery_string = $_POST[\'my-gallery\'];
if( !empty( $gallery_string ) ) {
//It\'s a string (e.g \'1424,3435,7544,7332\') - turn it into array
$gallery_array = explode(\',\', $gallery_string);
foreach( $gallery_array as $value ) {
//Full path of attachment
$filename = get_attached_file( $value );
//Path to the upload directory
$wp_upload_dir = wp_upload_dir();
//Type of file
$filetype = wp_check_filetype( basename( $filename ), null );
//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, $target_post_id );
//Needed for wp_generate_attachment_metadata()
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 );
}
}
我知道我可以使用相同的图像ID
wp_delete_attachment()
在“复制”后删除,但仍然。。如果(
IF) 有没有一种方法可以连接它?
我还调查了upload.php
在wp admin文件夹中,当您按下媒体页面中的“附加”按钮时,会触发该操作,但它似乎无法用于我们的代码中(如果是,则需要相当大的麻烦)。