Ich habe eine Funktion, die alle Bilder in der WordPress-Medienbibliothek zurückgibt, aber das Problem ist, dass nicht alle Bildgrößen des Bildes zurückgegeben werden, z. B .: (Miniaturansicht, klein, groß), sondern nur das Originalbild.
function get_images_highcompress_data()
{
$args = array(
'post_type' => 'attachment',
'post_mime_type' => 'image/jpeg,image/jpg,image/png',
'post_status' => 'inherit',
'posts_per_page' => -1,
'orderby' => 'id',
'order' => 'ASC'
);
$query_images = new WP_Query( $args );
$images = array();
foreach ( $query_images->posts as $image) {
$images[]= $image->guid;
}
Gibt es eine andere Funktion, die alle Bilder-URLs mit allen Größen aus der WP-Medienbibliothek abrufen kann?.
Z.B. Image01.jpg
, image01-500X500.jpg
, Image01-1080X1080.jpg
, Image02.jpg
, image02-500X500.jpg
, Image02-1080X1080.jpg
So in einem einzigen Array.
Folgendes können Sie tun.
Lassen Sie uns Ihren Code folgendermaßen ändern:
function get_images_highcompress_data() {
$args = array(
'post_type' => 'attachment',
'post_mime_type' => 'image/jpeg,image/jpg,image/png',
'post_status' => 'inherit',
'posts_per_page' => -1,
'orderby' => 'id',
'order' => 'ASC'
);
// Get all the available thumbnail sizes
$sizes = get_intermediate_image_sizes();
// Query the attachments
$query_images = new WP_Query( $args );
$images = array();
// Run a loop
if ( $query_images->have_posts() ){
while ($query_images->have_posts()){
$query_images->the_post();
// For each attachment size, store its URL in an array
foreach ( $sizes as $key => $size ) {
$thumbnails[$key] = wp_get_attachment_image_src( get_the_ID(), $size)[0];
}
$images = array_merge( $thumbnails , $images );
}
return $images;
}
}
Das zurückgegebene Array sieht folgendermaßen aus:
[0] => thumbnail-url,
[1] => medium-url,
[2] => large-url,
Und so weiter.