break;
endforeach;
-$k++;
// If there is more than 1 attachment in a gallery
if ( count( $attachments ) > 1 ) :
+ $k++;
if ( isset( $attachments[ $k ] ) ) :
// get the URL of the next image attachment
$next_attachment_url = get_attachment_link( $attachments[ $k ]->ID );