If you were to view one of the images in my WordPress gallery, such as my Minnesota State Fair – 2009 gallery, and view one of the images. You will see a previous and next image on the bottom of the image page. As you may of already of guessed, the next and previous images are all part of the way my theme handles image pages.
In order to do this, I needed to add the below snippet to my image.php file. You should already have a “navigation” section, find it, and replace it with the below code (MAKE SURE YOU BACK UP YOUR IMAGE.PHP FILE FIRST).
<div class="image-navigation">
<?php $attachments = array_values(get_children( array('post_parent' => $post->post_parent, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID') ));
foreach ( $attachments as $k => $attachment )
if ( $attachment->ID == $post->ID )
break;
$attachments = array_values(get_children( array('post_parent' => $post->post_parent, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID') ));
$next_url = isset($attachments[$k+1]) ? get_permalink($attachments[$k+1]->ID) : get_permalink($attachments[0]->ID);
$previous_url = isset($attachments[$k-1]) ? get_permalink($attachments[$k-1]->ID) : get_permalink($attachments[0]->ID);
if ( wp_get_attachment_image( $post->ID+1 ) != null ) { ?>
<p class="attachment">
Next Image<br />
<a href="<?php echo $next_url; ?>"><?php echo wp_get_attachment_image( $post->ID+1, 'thumbnail' ); ?></a>
</p>
<?php }
if ( wp_get_attachment_image( $post->ID-1 ) != null ) { ?>
<p class="attachment">
Previous Image<br />
<a href="<?php echo $previous_url; ?>"><?php echo wp_get_attachment_image( $post->ID-1, 'thumbnail' ); ?></a>
</p>
<?php } ?>
</div>


Thank you for posting your code! I was searching everywhere for the next and previous attachment algorithm and landed on your site. Coincidentally, I’m from MN as well. =)
Used the code for the right and left navi arrows!
check it out: http://kennyhoang.com/?attachment_id=682
I’m glad I could help.
Hi Matt,
Are you using Gallery as a backend to display the photo EXIF data? I noticed you had a Gallery install in addition to a default WordPress Gallery. I ran a Gallery install for a while before switching to Flickr (server wouldn’t fun Gallery 2) and am now thinking that I may just go back to using the default WordPress media library and gallery. May I ask why you’re using both Gallery and WordPress? Thanks!
Chad
To display the EXIF data, I’m using a plugin called WP Gallery EXIF Reader. It works pretty will but isn’t the most flexible (out of the box) as far as the look and feel of the data.
As to why Im using both Gallery 2/3 and WordPress. I haven’t figured out how I want to display my pictures yet, so im still playing with ides. But have you had a look at the new Gallery 3, it’s pretty cool.