1 回答

TA貢獻1875條經驗 獲得超3個贊
解決方案是在wp_delete_attachment后有條件地使用取消鏈接,以防生成的PDF與已刪除的PDF具有相同的名稱。在這種情況下,不需要刪除附件或取消鏈接,只需覆蓋現有文件即可。
對于更新帖子:
$savePath = trailingslashit($uploads_dir).$new_file_name.'.pdf';
$pdf->Output($savePath, 'F');
$existing_PDF = get_attached_media('application/pdf', $post_id);
foreach($existing_PDF as $pdf):
$oldPDFID= $pdf->ID;
$file = get_attached_file($oldPDFID, true);
$old_file_name = pathinfo($file,PATHINFO_BASENAME);
endforeach;
}
$old_pdf = pathinfo($file,PATHINFO_BASENAME);
#######CREATE PDF ATTACHMENT####
$args = array(
'post_title' => "$new_file_name",
'post_content' => '',
'post_mime_type' => 'application/pdf',
);
$new_file_name = $new_file_name.".pdf";
//DELETE OLD ATTACHMENT
if($new_file_name != $old_file_name):
wp_delete_attachment($oldPDFID, true);
unlink($file);
endif;
$pdfID = wp_insert_attachment($args, $savePath, $post_id);
- 1 回答
- 0 關注
- 113 瀏覽
添加回答
舉報