Image gallery based on post attachments
-
Hey, folks. I’m trying to set up a basic image gallery on my blog (reanimate.ca). It’s an art blog, so basically it’s set up so that each post has its own image.
For ease of navigation, I’m trying to set up a thumbnail gallery, in which each thumbnail will link to its respective post, instead of just a larger JPG.
I’m very new to PHP and programming in general, so a lot of my coding is probably very messy.
Here’s what I’ve been trying for the gallery. This is copied almost directly from the get_posts listing in the Codex.
<ul> <?php global $post; $myposts = get_posts('numberposts=-1&category=4'); foreach($myposts as $post) : ?> <li> <a href="<?php the_permalink(); ?>"> <!-- Instead of the post title, I want to insert a thumbnail from the image attached the this post. I tried using "wp_get_attachment_thumb_file()" but I don't know how to call the attachment ID from each post. --> <?php the_title(); ?> </a> </li> <?php endforeach; ?> </ul>
This behaves almost exactly the way I want my gallery to behave. I’m going to style it so that the list is inline, but aside from that, it works perfectly. Except, of course, that I can’t get a thumbnail image for the link.
I’m assuming that wp_get_attachment_thumb_file() will work for what I need, but I don’t know how to call the attachment ID. I’ve tried a couple things here and there, but my knowledge level is far too low to get any further.
I tried another method from the same get_posts codex entry, but it seemed messier, and harder to control:
<?php $args = array( 'post_type' => 'attachment', 'numberposts' => -1, ); $attachments = get_posts($args); if ($attachments) { foreach ($attachments as $post) { setup_postdata($post); the_attachment_link($post->ID, false); } } ?>
That’s almost exactly how it’s written out in the Codex, except that I’ve removed a couple of (I think) unnecessary code items. It gets me a thumbnail gallery, but I don’t know how I would style it. As well, the links that it returns are to the image files, as opposed to the posts that the images are attached to.
Basically, I’m not looking for anything complicated. I could easily hard-code an image gallery to work the way I want it to, but in the long run, it would be a lot more work – since I’ll have to update it every time I add a new post.
If you guys know of a simple solution (like how to call the attachment ID, for the first block of code), I’d be super greatful. If I’m mistaken, and it’s actually a lot more complicated than I’m guessing, then does anyone know of a simple plugin that would work? I don’t want to use Lightbox or Shutter or any of those guys – they’re neat, but a little too gimmicky for me.
Thanks a lot for your help, sorry for typing so much. Also sorry if this question has been answered somewhere else earlier! I’ve tried searching around, but haven’t found anything that quite fits my needs. Thanks, again.
- The topic ‘Image gallery based on post attachments’ is closed to new replies.