diff --git a/functions.php b/functions.php index 280ed95..ec43d7f 100644 --- a/functions.php +++ b/functions.php @@ -230,4 +230,49 @@ function add_allowed_tags($tags) { return $tags; } add_filter('wp_kses_allowed_html', 'add_allowed_tags'); + +/*the fallback from wp_nav_menu, we want to replicate the output of wp_page_menu to be similar to wp_nav_menu +@param args, it comes from the wp_nav_menu arguments*/ +function hale_main_nav_fallback($args) { + $args['echo'] = 0; // don't echo the output yet. + $nav_menu = wp_page_menu($args); + if(!empty($nav_menu)){ + $doc = new DOMDocument(); + @$doc->loadHTML($nav_menu);//surpress the warnings + $data = $doc->getElementsByTagName('li'); + //the default behaviour of wp_page_menu is that it wraps the list items with a ul and div + //menu_class is for div in wp_page_menu, but is for the ul in wp_nav_menu + //create the container, if any + //container class and id come from wp_nav_menu arguments. Which is not used in wp_page_menu. + if($args['container'] != false) { + $attributes = (!empty($args['container_class'])?' class="'.$args['container_class'].'"':''); + $attributes .= (!empty($args['container_id'])?' id="'.$args['container_id'].'"':''); + if($args['container'] == 'nav') { + echo ''; + } else { + echo ''; + } + + } + $attributes = (!empty($args['menu_class'])?' class="'.$args['menu_class'].'"':''); + $attributes .= (!empty($args['menu_id'])?' id="'.$args['menu_id'].'"':''); + echo ''; + if($data->length > 0) { + foreach($data as $item) { + echo $item->ownerDocument->saveXML($item);//saveHTML wouldn't accept it as paramater. + } + } + echo ''; + //close the container, if any + if($args['container'] != false) { + if($args['container'] == 'nav') { + echo ''; + } else { + echo ''; + } + + } + } + return; +} ?> \ No newline at end of file diff --git a/inc/nav-walker.php b/inc/nav-walker.php index cdf2485..605724b 100644 --- a/inc/nav-walker.php +++ b/inc/nav-walker.php @@ -344,13 +344,16 @@ function my_wp_nav_menu_objects_sub_menu( $sorted_menu_items, $args ) { $second_level_parent = $newmenu[3][$parent_id]; } else return array(); - foreach($newmenu[3] as $id=>$menu_item){ - if($menu_item != $second_level_parent) unset($newmenu[3][$id]); + if( isset($newmenu[3]) && is_array($newmenu[3]) ){ + foreach($newmenu[3] as $id=>$menu_item){ + if($menu_item != $second_level_parent) unset($newmenu[3][$id]); + } } - foreach($newmenu[4] as $id=>$menu_item){ - if(!isset($newmenu[3][$menu_item])) unset($newmenu[4][$id]); + if( isset($newmenu[4]) && is_array($newmenu[4]) ){ + foreach($newmenu[4] as $id=>$menu_item){ + if(!isset($newmenu[3][$menu_item])) unset($newmenu[4][$id]); + } } - foreach($sorted_menu_items as $key => $menu_item) { if(!isset($newmenu[3][$menu_item->ID]) && !isset($newmenu[4][$menu_item->ID])) unset($sorted_menu_items[$key]); } diff --git a/inc/nav.php b/inc/nav.php index 8cd58f0..161d746 100644 --- a/inc/nav.php +++ b/inc/nav.php @@ -14,19 +14,24 @@