The function below is derived from adjacent_image_link()
function. You can get both previous and next image source by this function. It can be used inside or outside loop.
Uses
// get next image src
$src = wpse145194_get_adjacent_image_link();
// get previous image src
$src = wpse145194_get_adjacent_image_link(true);
Function
function wpse145194_get_adjacent_image_link( $prev = false, $size="thumbnail", $post_id = false) {
if(!$post_id)
$post = get_post();
else
$post = get_post($post_id);
$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;
$k = $prev ? $k - 1 : $k + 1;
$link = $attachment_id = null;
if ( isset( $attachments[ $k ] ) ) {
$attachment_id = $attachments[ $k ]->ID;
// $link = wp_get_attachment_link( $attachment_id, $size, true, false, false );
$link = get_attachment_link( $attachment_id);
}
return $link;
}
Code not tested