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 have created a template for gallery named image.php which allows me to create a news website style image gallery that can be embedded in posts. But I need two more things to make this complete.

  • First, I want to know how to create a text link to the first image in a gallery, rather than displaying thumbnails for the gallery as the built-in [gallery] shortcode currently does. For example, check out "click here to start" on this page: http://www.businessinsider.com/android-vs-iphone-debate-quotes-2011-12

  • Secondly, if anybody knows how to generate a list of all the images in a gallery which I can add to my image.php file that would be great to have so I could use it to create a slider on the bottom of each page featuring an image with the other images in the gallery.

share|improve this question
    
Text link to the first image? The example you've given is an image containing text. And you want to fetch all the images from the current post gallery right? –  Joshua Abenazer Dec 3 '11 at 17:01
    
There's also a text link at the bottom of the article. And yes, fetch the images from the current post gallery. –  AndrettiMilas Dec 3 '11 at 21:19
    
+1 because that's exactly what i'm looking for! –  Tom Sarduy Dec 6 '11 at 2:46

1 Answer 1

up vote 3 down vote accepted
+50

Attachments in a gallery are their own posts, with some special settings. To get a list of all the attachments for a given post, you basically just create a new query and specify the post parent and attachment type.

$gallery_images = new WP_Query(array(
    'post_parent' => $post->ID,
    'post_type' => 'attachment',
    'post_mime_type' => 'image',
    'post_status' => 'inherit',
    )
);

You can then loop through this query in the same way as any other loop to access those images.

while ( $gallery_images->have_posts() ) : $gallery_images->the_post();
    the_title(); 
    the_content(); 
    the_permalink();
    whatever();
endwhile;

// always reset the post data at the end of any non-main loop
wp_reset_postdata();

Creating a text link to one of those images would be done the wp_get_attachment_link function. For a text only link, use "none" as the size parameter.

echo wp_get_attachment_link( $attachment_id, 'none', true, false, 'Link Text' );
share|improve this answer
    
The above code is not grabbing the other images for me. –  AndrettiMilas Dec 14 '11 at 19:45
    
I use code exactly like that on many sites, for many galleries. It works fine, but you'll obviously need to customize it for your particular setup. The main WP_Query though is basically correct and should work in a standard Loop. –  Otto Dec 14 '11 at 22:08
    
pastebin.com/2eSNW0Le Did I do this correctly? Lines 74 to 94. –  AndrettiMilas Dec 14 '11 at 22:12
    
No, that looks like you're putting this into a single image attachment page or something. In which case you should have 'post_parent' => $parent->ID,. Also code isn't copy-pasta, you need to modify it to be specific to your own needs. The fact that you left whatever(); in there kinda indicates that you don't really know what you're doing. I was trying to show you how it works, you still have to write your own code to do what you want. –  Otto Dec 14 '11 at 22:20
    
I definitely don't know what I'm doing with PHP. –  AndrettiMilas Dec 14 '11 at 22:23

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.