From 0223f0735fad20c790a49cabee4673a247207a5a Mon Sep 17 00:00:00 2001 From: Salman Date: Wed, 22 Oct 2014 11:52:39 -0400 Subject: [PATCH] Primary menu behavior Functions.php: Add primary location fallback Nav.php : Check if menu is empty --- functions.php | 45 +++++++++++++++++++++++++++++++++++++++++++++ inc/nav.php | 31 ++++++++++++++++++------------- 2 files changed, 63 insertions(+), 13 deletions(-) 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.php b/inc/nav.php index 8cd58f0..161d746 100644 --- a/inc/nav.php +++ b/inc/nav.php @@ -14,19 +14,24 @@