Skip to content

Instantly share code, notes, and snippets.

@designbuildtest
Last active August 29, 2015 14:05
Show Gist options
  • Save designbuildtest/3679ed35ee94e708bac1 to your computer and use it in GitHub Desktop.
Save designbuildtest/3679ed35ee94e708bac1 to your computer and use it in GitHub Desktop.
Image navigation
<?php
// http://wordpress.org/support/topic/previous_image_link
function mytheme_get_previous_image() {
$image = mytheme_adjacent_image_link( true );
if ( $image ) : ?>
<div class="nav-previous odd summary">
<div class="entry-meta entry-meta-above"><?php _e('Previous Image','sendtheme'); ?></div>
<div class="entry-image"><?php echo $image; ?></div>
</div><?php
endif;
}
function mytheme_get_next_image() {
$image = mytheme_adjacent_image_link( false );
if ( $image ) : ?>
<div class="nav-next even summary">
<div class="entry-meta entry-meta-above"><?php _e('Next Image','sendtheme'); ?></div>
<div class="entry-image"><?php echo $image; ?></div>
</div><?php
endif;
}
function mytheme_adjacent_image_link( $prev = true ) {
global $post;
$post = get_post( $post );
$attachments = array_values( get_children(
array('post_parent' => $post->post_parent,
'post_type' => 'attachment',
'post_mime_type' => 'image',
'orderby' => 'menu_order ASC, ID ASC'
)
) );
foreach ( $attachments as $k => $attachment )
if ( $attachment->ID == $post->ID ) :
break;
endif;
$k = $prev ? $k - 1 : $k + 1;
if ( isset($attachments[$k]) ) :
return wp_get_attachment_link($attachments[$k]->ID, 'medium', true);
else :
return false;
endif;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment