next_post_link() on custom menu structure

The function behind the function behind the function…

next_post_link(); is a wrapper for adjacent_post_link(); which then calls get_adjacent_post(); where the 3rd argument is $previous and set to true/false to get the previous or next post.

This gives you two opportunities: Use the output or the query filter.

Output filter

This might sound easier, but you’ll have to do an extra query for this.

// The core:
$adjacent = $previous ? 'previous' : 'next';
echo apply_filters( "{$adjacent}_post_link", $format, $link );

// Add your callbacks like this:
add_filter( 'previous_post_link', 'your_callback_fn', 20, 2 );
add_filter( 'next_post_link', 'your_callback_fn', 20, 2 );

Query filter

This is what I’d recommend you: Check whether there’s $post->post_parent set and then modify the query to get the post with the next higher id that has the same parent.

// The core:
$adjacent = $previous ? 'previous' : 'next';
$join  = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_cat, $excluded_categories );
$where = apply_filters( "get_{$adjacent}_post_where", $wpdb->prepare("WHERE p.post_date $op %s AND p.post_type = %s AND p.post_status="publish" $posts_in_ex_cats_sql", $current_post_date, $post->post_type), $in_same_cat, $excluded_categories );
$sort  = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1" );