Skip to content

Instantly share code, notes, and snippets.

@sagarjadhav
Created March 5, 2013 09:19
Show Gist options
  • Save sagarjadhav/5089013 to your computer and use it in GitHub Desktop.
Save sagarjadhav/5089013 to your computer and use it in GitHub Desktop.
Get Child pages of page according to their menu order in nav menu
<?php
/**
* Get Child pages of page according to their menu order in nav menu
*
* @return array ids of child pages order by their order in menu. FALSE on failure.
*/
function get_menu_child_pages() {
$menu_id = 0;
$current_page_id = get_the_ID();
$child_menu_list = '';
$menu_parent_id = '';
$all_menu_loc = get_nav_menu_locations();
if ( is_array( $all_menu_loc ) && $all_menu_loc && key_exists( 'primary', $all_menu_loc ) ) {
$menu_id = $all_menu_loc['primary'];
}
if ( $menu_id && $current_page_id ) {
$main_nav_menu = wp_get_nav_menu_items( $menu_id, array( 'orderby' => 'menu_order' ) );
if ( is_array( $main_nav_menu ) && !empty( $main_nav_menu ) ) {
foreach ( $main_nav_menu as $nav_item ) {
$nav_object = $nav_item->object;
if ( isset( $nav_object ) && ( $nav_object == 'page' ) ) {
if( $nav_item->object_id == $current_page_id && $menu_parent_id == '' ) {
$menu_parent_id = $nav_item->ID;
}
if( isset( $nav_item -> menu_item_parent ) && $nav_item -> menu_item_parent == $menu_parent_id ) {
$child_menu_list[] = $nav_item->object_id;
}
}
}
}
}
if ( !empty( $child_menu_list ) ) {
return $child_menu_list;
} else {
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment