解决方案 1:首先查询帖子,然后为每个帖子使用类似以下代码:
$images = get_children( array(
'post_parent' => get_the_ID(),
'post_type' => 'attachment',
'post_mime_type' => 'image',
'numberposts' => -1
) );
foreach ( (array) $images as $image ) {
print wp_get_attachment_url( $image->ID );
}
我不喜欢这个解决方案,因为它会生成许多 SQL 查询。
解决方案 2:编写您自己的 SQL 查询,该查询将一次选择所有图像。它应该看起来像这样:
// change this to your custom post type, attachment in your case.
$post_type = 'attachment';
global $wpdb;
$where = get_posts_by_author_sql( $post_type );
$query = "SELECT * FROM $wpdb->posts p where p.post_type = 'attachment' AND (p.post_mime_type LIKE 'image/%') AND (p.post_status = 'inherit') AND p.post_parent IN (SELECT $wpdb->posts.ID FROM $wpdb->posts {$where} ) ORDER BY p.post_date DESC";
$results = $wpdb->get_results( $query );
if ( $results ) {
foreach ( (array) $results as $image ) {
print wp_get_attachment_url( $image->ID );
}
}
我构建该插件的方式是创建一个自定义帖子类型,类似于媒体上传类型,然后根据需要对其进行修改(例如添加一个类别)。
我喜欢插件的想法,所以让我知道然后你完成,祝你好运。
编辑:如果您真的想将所有图像文件复制到另一个目录,您可以使用以下方法执行以下操作:
$source = wp_upload_dir(); // get WordPress upload directory
$dest= "wp-content/My-new-Directory"; // where you would like to copy
mkdir($dest, 0755);
foreach (
$iterator = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($source, \RecursiveDirectoryIterator::SKIP_DOTS),
\RecursiveIteratorIterator::SELF_FIRST) as $item
) {
if ($item->isDir()) {
mkdir($dest . DIRECTORY_SEPARATOR . $iterator->getSubPathName());
} else {
copy($item, $dest . DIRECTORY_SEPARATOR . $iterator->getSubPathName());
}
}