功能介绍

站点内的文章肯定不止一篇两篇,媒体内的图片肯定也是不止一张两张,当我想删除一篇文章时又会头疼这个图片怎么去删除,以下方法就很好的去解决这个问题,当我去删除这篇文章时,这篇文章内的图片也会同步删除!

配置教程

首先利用宝塔或则FTP打开/inc/options下的admin-options.php文件把下面代码放在20行左右

array(
'id' => 'is_delete_article',
'type' => 'switcher',
'title' => '删除文章同时删除图片',
'label' => '启用后在删除文章时会删除文章对应的图片',
'default' => false,
),

最后在主题根目录下打开functions.php将以下代码放在最后即可

//+----------------------------------------------------------------------
//| 删除文章时删除图片附件
//+----------------------------------------------------------------------
function delete_post_and_attachments($post_ID) {
global $wpdb;
//删除特色图片
$thumbnails = $wpdb->get_results( "SELECT * FROM $wpdb->postmeta WHERE meta_key = '_thumbnail_id' AND post_id = $post_ID" );
foreach ( $thumbnails as $thumbnail ) {
wp_delete_attachment( $thumbnail->meta_value, true );
}
//删除图片附件
$attachments = $wpdb->get_results( "SELECT * FROM $wpdb->posts WHERE post_parent = $post_ID AND post_type = 'attachment'" );
foreach ( $attachments as $attachment ) {
wp_delete_attachment( $attachment->ID, true );
}
$wpdb->query( "DELETE FROM $wpdb->postmeta WHERE meta_key = '_thumbnail_id' AND post_id = $post_ID" );
}
if(_cao('is_delete_article') ){
add_action('before_delete_post', 'delete_post_and_attachments');
}
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。