• Resolved cleinonen

    (@cleinonen)


    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.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter cleinonen

    (@cleinonen)

    Bump?
    Anyone have any ideas about this?

    I’ve also been thinking about an alternative method of doing things, that is, setting up each category for the gallery by bringing each thumbnail from The Loop – but how do I get the loop to look through all my posts, when I’m on a “static” page?

    Anyway, thanks for taking a second look…

    It’s been a while since you posted this. Don’t know if you already got this. If you haven’t, here’s a snip that may help you

    $args = array(
        'post_type' => 'attachment',
        'numberposts' => -1, // bring them all
        'post_status' => null,
        'post_parent' => $post->ID // post id with the gallery
        ); 
    
    $attachments = get_posts($args);

    There. $attachments now is an Array of objects containing all the post attachments. You may add an extra arg stating that you want only images (because you can attach videos too).

    Hope this helps.

    Cheers

    Ricardo Valfreixo
    minimalistic studios

    Thread Starter cleinonen

    (@cleinonen)

    I don’t want to bump this, because the problem’s been solved for a while – but I figured I might as well add a note about how I solved it, in case others are looking for the same solution.

    Basically, I just used The Attached Image plugin in multiple loops (to sort by category). Super simple.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Image gallery based on post attachments’ is closed to new replies.