Next previous link in menu

In some cases you need to know the next and previous url of your current post in a WordPress menu. With the following function you can accomplish this.

function gtp_menu_slide_link( $menu_name, $return = null ) {
	
	global $post;
	$locations = get_nav_menu_locations();
	$menu = wp_get_nav_menu_object( $locations[$menu_name] );
	$menu_items = wp_get_nav_menu_items( $menu->term_id, array( 'order' => 'DESC' ) );

	if ( ! isset( $links ) ) $links = null;
	if ( ! isset( $urls ) ) $urls = null;

	$i = -1;
	foreach ( $menu_items as $item ) {
		
		$i++;
		$id 	= get_post_meta( $item->, '_menu_item_object_id', true );
		$link = get_page_link( $id );

		$links .= $id . ',';
		$urls  .= $link . ',';

		if ( $id == $post-> ) {
			$prev_id = $i - 1;
			$next_id = $i + 1;
		}
	}

	$links 	= explode( ',', $links );
	$urls 	= explode( ',', $urls );

	if ( array_key_exists( $next_id, $urls ) ) {
		$next_id = $urls[$next_id];
	} else {
		$next_id = false;
	}

	if ( array_key_exists( $prev_id, $urls ) ) {
		$prev_id = $urls[$prev_id];
	} else {
		$prev_id = false;
	}

	switch ( $return ) {
		case 'next' :
			return $next_id;
			break;
		case 'previous' :
			return $prev_id;
			break;
	}
	
}