<?php
$menu_name = \'primary\';
$locations = get_nav_menu_locations();
$menu = wp_get_nav_menu_object( $locations[ $menu_name ] );
$menuitems = wp_get_nav_menu_items( $menu->term_id, array( \'order\' => \'DESC\' )
);
$i=-1;
foreach ( $menuitems as $item ):
$i++;
$id = get_post_meta( $item->ID, \'_menu_item_object_id\', true );
$page = get_page( $id );
$link = get_page_link( $id );
$title = get_the_title( $id );
$linkarray.=$id.",";
$urlarray.=$link.",";
$titlearray.=$title.",";
if ($id==$post->ID){
$previd=$i-1;
$nextid=$i+1;
$titleid=$i+1;
}
endforeach;
$linkarray=explode(\',\',$linkarray);
$urlarray=explode(\',\',$urlarray);
$titlearray=explode(\',\',$titlearray);
$titleid=$titlearray[$titleid];
$nextid=$urlarray[$nextid];
if (empty($nextid)){
$nextid=$urlarray[0];
}
$previd=$urlarray[$previd];
if (empty($previd)){
$previd=$urlarray[$i];
}
?>
<?php echo $titleid; ?>
This is the edited working version, in case if somebody will need.