WordPress: Click an attachment image to view next image in order

Add the following to the top of attachment.php


<?php
// code copied from adjacent_image_link() in wp-include/media.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;

$next_url =  isset($attachments[$k+1]) ? get_permalink($attachments[$k+1]->ID) : get_permalink($attachments[0]->ID);
?>

Find your attachment image code in attachment.php


<p class="attachment"><a href="<?php echo $next_url; ?>"><?php echo wp_get_attachment_image( $post->ID, $size='fullsize' ); ?></a></p>

%d bloggers like this: