Show WordPress Media library images on a single page

Show WordPress Media library images on a single page

Show WordPress Media library images on a single page

Recently i was working on a gallery based wordpress website, that was prior to wordpress 3.5 released and i need to show media library images on a single page in wordpress, rather than showing posts attachement or posts thumbnails. I Googled it and found little help on how to fix this. When i found a solution after experimenting with media library functions i thought i should share my experience with you.

I am sure you understand the difference between wordpress blog and wordpress pages and i am also assuming that you know how to upload images to media library either through posts, pages or directly to media. So here is the code that will do the trick

   $media_query = new WP_Query(
   array(
        'post_type' => 'attachment',
        'post_status' => 'inherit',
        'posts_per_page' => -1,
       )
   );
   $list = array();
   foreach ($media_query->posts as $post) {
?>
<a href="<?php //the_permalink(); ?>"><img src="<?php echo $list[] = wp_get_attachment_url($post->ID); ?>"></a>

this small code will be very handy when you need WordPress Media library images shown on a page as gallery or slider. I used this code in more depth for my project, if you need more help on how to show wordpress media library images or you have any question related to media library uses please feel free to ask me questions on either facebook or through comments here.

You may play with this code, and share it how you use it and help others learn..