diff --git a/wp-includes/media.php b/wp-includes/media.php index f9b47e4c90..43b5aab4f9 100644 --- a/wp-includes/media.php +++ b/wp-includes/media.php @@ -1902,16 +1902,22 @@ function adjacent_image_link($prev = true, $size = 'thumbnail', $text = false) { $post = get_post(); $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 ) + foreach ( $attachments as $k => $attachment ) { + if ( $attachment->ID == $post->ID ) { break; + } + } - $k = $prev ? $k - 1 : $k + 1; + $output = ''; + $attachment_id = 0; - $output = $attachment_id = null; - if ( isset( $attachments[ $k ] ) ) { - $attachment_id = $attachments[ $k ]->ID; - $output = wp_get_attachment_link( $attachment_id, $size, true, false, $text ); + if ( $attachments ) { + $k = $prev ? $k - 1 : $k + 1; + + if ( isset( $attachments[ $k ] ) ) { + $attachment_id = $attachments[ $k ]->ID; + $output = wp_get_attachment_link( $attachment_id, $size, true, false, $text ); + } } $adjacent = $prev ? 'previous' : 'next';