Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I want to grab images 161 thru 166 without having to calling my entire media library array and having to splice those up. In time, the more I have in there the more it's going to slow my site down. Here is what I have so far, I use array_reverse to reverse the IDs so the most recent uploads are last and I use array_splice to find the images I need to extract. Is there a more direct way for me to find images with ID 161 thru 166?

function get_images_from_media_library() {
                $args = array(
                    'post_type' => 'attachment',
                    'post_mime_type' =>'image',
                    'post_status' => 'inherit',
                    'posts_per_page' => -1,
                );
                $query_images = new WP_Query( $args );
                $images = array();
                foreach ( $query_images->posts as $image) {
                    $images[]= $image->guid;
                }
                $images = array_reverse($images);
                $images = array_splice($images, 3,6);
                return $images;
            }

            $img = get_images_from_media_library();
            foreach($img as $image){
                echo "<img src='$image'/>";
            }
share|improve this question
add comment

2 Answers

up vote 0 down vote accepted

Add post__in (two underscores) to your $args:

$args = array(
    'post_type' => 'attachment',
    'post_mime_type' =>'image',
    'post_status' => 'inherit',
    'posts_per_page' => -1,
    'post__in' => array( 161, 162, 163, 164, 165, 166 ),
);

Reference

WP_Query on the Codex

share|improve this answer
    
Ahh, I was looking at the wrong WP reference pages. Thanks, I've bookmarked WP_Query, now will be referring to that more. –  GivenPie Jul 16 '13 at 13:49
    
I generally have about three or ten tabs open to Codex pages when I'm developing WP stuff. –  Pat J Jul 16 '13 at 13:54
add comment

If you have all the IDs of images you want then you just need to use the wp_get_attachment_image() function in your loop.

Create an array of all the required ids and iterate through the array applying the above function which will show all the images.

Ex.

wp_get_attachment_image($post->ID, /*size of image*/);

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.