我面临着一个我无法解决的恼人问题。
我编写此代码是为了在自定义帖子类型上添加或更新附件:
function add_update_attachment( $postID, $photo ) {
if ($photo == \'\') return;
$basename = basename($photo);
$basenameExp = explode("?", $basename);
$name = $basenameExp[0];
$attach = array_shift(get_attached_media(\'image\', $postID));
$attachName = basename( $attach->guid );
$wp_upload_dir = wp_upload_dir();
$fileName = $wp_upload_dir[\'path\']. "/".urldecode($name);
$fileType = wp_check_filetype( $name, null );
// echo \'<pre>\'.print_r($wp_upload_dir, true).\'</pre>\';
$attachment = array(
\'guid\' => $wp_upload_dir[\'url\'] . \'/\' . $name ,
\'post_mime_type\' => $fileType[\'type\'],
\'post_title\' => preg_replace( \'/\\.[^.]+$/\', \'\', $name ),
\'post_content\' => \'\',
\'post_status\' => \'inherit\'
);
if ( str_replace(\'%20\', \' \', $attachName) !== $photo ) {
wp_update_post( array( \'ID\' => $attach->ID, \'post_parent\' => 0) );
copy ( $photo, $wp_upload_dir[\'path\']. "/" . urldecode($name) );
$attachID = wp_insert_attachment( $attachment, $fileName, $postID );
$attachData = wp_generate_attachment_metadata( $attachID, $fileName );
wp_update_attachment_metadata( $attachID, $attachData );
set_post_thumbnail( $postID, $attachID );
}
}
问题是:我的一些帖子是无缘无故复制的。
我有这个帖子(我只显示一个自定义字段):
Array
(
[0] => LieuTemp001
[1] => LieuTemp002
)
最后一个是复制的。为什么?如果我不使用这个函数,我没有问题(但我需要明显地使用它)