Matt's Technology Blog

A place to play with code, software, & the web

Main menu

Skip to primary content
Skip to secondary content
  • Home
  • About
  • Contact Me
  • Projects
    • Favicon WordPress Plugin
    • Random Image Block Plugin
    • Sitemap Generator WordPress Plugin
    • Top Posts & Pages Widget
    • Typekit Font’s WordPress Plugin
    • Webmaster Tools WordPress Plugin

Post navigation

← Previous Next →

Next Gallery Image Link in Image Posts

Posted on October 27, 2009 by Matt Rude

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>
  • Share this:
  • Facebook
  • Reddit
  • Digg
  • Share
  • Email
  • Print
  • StumbleUpon
This entry was posted in How-To's and tagged HTML element, Languages, Metadata, php, Programming, WordPress, WordPress Plugin Directory by Matt Rude. Bookmark the permalink.
Logging In...

Profile cancel

Sign in with Twitter Sign in with Facebook
or

Not published

  • 7 Replies
  • 7 Comments
  • 0 Tweets
  • 0 Facebook
  • 0 Pingbacks
Last reply was September 11, 2010
  1. Kenny
    View November 10, 2009

    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

    Reply
    • Matt Rudereplied:
      View November 11, 2009

      I’m glad I could help.

      Reply
  2. Chad
    View November 24, 2009

    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!

    Reply
    • Matt Rudereplied:
      View November 24, 2009

      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.

      Reply
  3. Birks
    View May 10, 2010

    Hello, this may be a stupid question, but what is the difference between using your solution, and the code that is already in image.php:

    Thanks!

    Reply
    • Matt Rudereplied:
      View May 10, 2010

      My solution displays the actual image for the next and previous images.

      Reply
  4. Carsten
    View September 11, 2010

    Hello Matt I am looking for a solution to link the main photo directly as download-link on a thumbnail.
    Is that possible? Where I have to change that in the code? Maybe you have a solution …?

    thanxx carsten

    Reply

Matt Rude Hi, my name is Matt Rude, I’m a 31 year old,living in the Twin Cities, Minnesota. I have worked in Information Technology for over 15 years. During that time I have worked for business sectors as diverse as Public Schools to Auto Part to Health Care.

Links

  • Matt Rude
  • My Github Repository
  • My Personal Wiki
  • My Twitter Archive
  • Personal Git Repository

Top Posts

  • Creating a secure Git repository server
  • Enable SSH Key Authorization on FreeNAS
  • Setting the Time Zone on Fedora from the shell
  • LDAP PHP Change Password Page
  • Configuring Spamassassin (MySQL & Virtual Users)
  • Run a Secure git Repository on FreeNAS
  • SSH with no password
  • Git Commit eMail Notifications

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

RSS Feed RSS - Posts

RSS Feed RSS - Comments

Categories

  • Asides (2)
  • General (14)
  • How-To's (64)
  • Notes (2)
  • Plugins (8)
    • RoundCube (1)
    • WordPress (7)
  • Python (1)
  • Scripts (8)
    • Bash (3)
    • PHP (2)
    • VBScript (3)
Proudly powered by WordPress

This page took 0.244 seconds of computer labor,
and required 10 questions to produce.

loading Cancel
Post was not sent - check your email addresses!
Email check failed, please try again
Sorry, your blog cannot share posts by email.