Index: sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/inc/template-tags.php =================================================================== --- sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/inc/template-tags.php (revision 875) +++ sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/inc/template-tags.php (revision 922) @@ -73,4 +73,82 @@ endif; + if ( ! function_exists( 'wporg_developer_post_nav_via_menu' ) ) : + /** + * Outputs previous and/or next post navigation links using the + * specified menu to inform navigation ordering. + * + * @param string $menu_name The name of the menu to use for nav ordering. + */ + function wporg_developer_post_nav_via_menu( $menu_name ) { + // Get the items for the specified menu + if ( ! $menu_items = wp_get_nav_menu_items( $menu_name ) ) { + return; + } + + // Get ids for all menu objects + $menu_ids = wp_list_pluck( $menu_items, 'object_id' ); + + // Get current post + if ( ! $post = get_post() ) { + return; + } + + // Index of current post in menu. Return if not in menu. + $i = array_search( $post->ID, $menu_ids ); + if ( false === $i ) { + return; + } + + // Find the previous post (note: preview menu item may not be a post) + $previous = null; + for ( $n = $i-1; $n >= 0; $n-- ) { + if ( isset( $menu_items[ $n ] ) && is_a( $menu_items[ $n ], 'WP_Post' ) ) { + $previous = $menu_items[ $n ]; + break; + } + } + + // Find the next post (note: next menu item may not be a post) + $next = null; + $max = count( $menu_items ); + for ( $n = $i+1; $n < $max; $n++ ) { + if ( isset( $menu_items[ $n ] ) && is_a( $menu_items[ $n ], 'WP_Post' ) ) { + $next = $menu_items[ $n ]; + break; + } + } + + if ( ! $previous && ! $next ) { + return; + } + ?> + + + +