Get All Images in Media Gallery?

admin2025-06-02  1

Is there a way to fetch the URLs of ALL images in the media gallery?

I think this would be an easy way for a website to have a Pictures page that just pulls all of the images from the media gallery, granted it would only be necessary in certain scenarios.

I don't need instructions on how to create a Pictures page, just how to pull all of the image URLs. Thanks!

Is there a way to fetch the URLs of ALL images in the media gallery?

I think this would be an easy way for a website to have a Pictures page that just pulls all of the images from the media gallery, granted it would only be necessary in certain scenarios.

I don't need instructions on how to create a Pictures page, just how to pull all of the image URLs. Thanks!

Share Improve this question edited Jan 20, 2012 at 23:23 Jared asked Mar 10, 2011 at 11:32 JaredJared 3,8453 gold badges35 silver badges58 bronze badges 1
  • Do you mean all images in the entire Media library (i.e., site-wide)? – ZaMoose Commented Mar 12, 2011 at 15:44
Add a comment  | 

6 Answers 6

Reset to default 72 +50
$query_images_args = array(
    'post_type'      => 'attachment',
    'post_mime_type' => 'image',
    'post_status'    => 'inherit',
    'posts_per_page' => - 1,
);

$query_images = new WP_Query( $query_images_args );

$images = array();
foreach ( $query_images->posts as $image ) {
    $images[] = wp_get_attachment_url( $image->ID );
}

All the images url are now in $images;

$media_query = new WP_Query(
    array(
        'post_type' => 'attachment',
        'post_status' => 'inherit',
        'posts_per_page' => -1,
    )
);
$list = array();
foreach ($media_query->posts as $post) {
    $list[] = wp_get_attachment_url($post->ID);
}
// do something with $list here;

Query the db for all media library items (not just ones attached to posts), grab their url, dump them all in $list array.

<?php
    $attachments = get_children( array('post_parent' => get_the_ID(), 'post_type' => 'attachment', 'post_mime_type' =>'image') );
    foreach ( $attachments as $attachment_id => $attachment ) {
            echo wp_get_attachment_image( $attachment_id, 'medium' );
    }
?>

This pulls all attachments for a post/page. Attach more images to a post, and it will be listed

ok y used this code for show ALL images in media Library!

$args = array(
    'post_type' => 'attachment',
    'post_status' => 'published',
    'posts_per_page' =>25,
    'post_parent' => 210, // Post-> ID;
    'numberposts' => null,
);

$attachments = get_posts($args);

$post_count = count ($attachments);

if ($attachments) {
    foreach ($attachments as $attachment) {
    echo "<div class=\"post photo col3\">";
        $url = get_attachment_link($attachment->ID);// extraigo la _posturl del attachmnet      
        $img = wp_get_attachment_url($attachment->ID);
        $title = get_the_title($attachment->post_parent);//extraigo titulo
        echo '<a href="'.$url.'"><img title="'.$title.'" src="'.get_bloginfo('template_url').'/timthumb.php?src='.$img.'&w=350&h=500&zc=3"></a>';
        echo "</div>";
    }   
}

and if you know method for show pagination, please answer.

This is just a shorter version of this answer using get_posts() and array_map().

$image_ids = get_posts(
    array(
        'post_type'      => 'attachment',
        'post_mime_type' => 'image',
        'post_status'    => 'inherit',
        'posts_per_page' => - 1,
        'fields'         => 'ids',
    ) );

$images = array_map( "wp_get_attachment_url", $image_ids );

It looks as though it hasn't been updated in a while, but the Media Library Gallery plugin might be a good example to start looking at.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748842047a314175.html

最新回复(0)