我在从帖子中卸载媒体时遇到问题,无法将其完全从我的网站中删除。例如,有没有一种简单的方法可以从帖子中取消附加图像?
我如何才能将媒体从帖子中移除?
3 个回复
最合适的回答,由SO网友:Jeremy Jared 整理而成
我从Brian那里学到了Fegter 您可以在参与此问题时从帖子中取消附加图像:Can I attach an image to a different post?
这并不完全“简单”,但也不难。这是我能找到的唯一解决办法。
SO网友:Low
这就是你要找的吗?
e.g. Remove all image attachments from a post
//get all image attachments
$attachments = get_children(
array(
\'post_parent\' => $post->ID,
\'post_mime_type\' => \'image\',
\'post_type\' => \'attachment\'
)
);
//loop through the array
if( !empty( $attachments ) ){
foreach( $attachments as $attachment ){
// Update the post into the database
wp_update_post( array(
\'ID\' => $attachment->ID,
\'post_parent\' => 0
)
);
}
}
但是,请注意caution 使用wp\\u update\\u post时。Alternate method using $wpdb
//replace this with the above inside the foreach block;
global $wpdb;
$wpdb->query(
"
UPDATE $wpdb->posts
SET post_parent = 0
WHERE ID = $attachment->id
AND post_type = \'attachment\'
"
);
SO网友:Tamlyn
从WordPress 4.2开始,此功能是内置的。单击Detach
媒体列表视图中的链接。
对于较旧版本,有一个插件可以执行相同的操作:https://wordpress.org/plugins/unattach/
Unattach是一个非常简单的插件,它允许从WordPress帖子、页面和其他内容类型中分离图像和其他媒体。
结束