Skip to content
This repository has been archived by the owner. It is now read-only.

Commit

Permalink
Primary menu behavior
Browse files Browse the repository at this point in the history
Functions.php: Add primary location fallback
Nav.php : Check if menu is empty
  • Loading branch information
szk11001 committed Oct 22, 2014
1 parent 42f77f7 commit 0223f07
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 13 deletions.
45 changes: 45 additions & 0 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 '<nav'.$attributes.'>';
} else {
echo '<div'.$attributes.'>';
}

}
$attributes = (!empty($args['menu_class'])?' class="'.$args['menu_class'].'"':'');
$attributes .= (!empty($args['menu_id'])?' id="'.$args['menu_id'].'"':'');
echo '<ul'.$attributes.'>';
if($data->length > 0) {
foreach($data as $item) {
echo $item->ownerDocument->saveXML($item);//saveHTML wouldn't accept it as paramater.
}
}
echo '</ul>';
//close the container, if any
if($args['container'] != false) {
if($args['container'] == 'nav') {
echo '</nav>';
} else {
echo '</div>';
}

}
}
return;
}
?>
31 changes: 18 additions & 13 deletions inc/nav.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,24 @@
<a class="skip-link screen-reader-text sr-only" href="#content">
<?php _e( 'Skip to content', 'cs' ); ?></a>
<div class="collapse navbar-collapse" id="primary-nav">
<?php
wp_nav_menu(
array(
'menu' => 'primary', /* menu name */
'menu_class' => 'nav navbar-nav'.$nav1.' '.$nav2,
'theme_location' => 'primary', /* where in the theme it's assigned */
'container' => false, /* container class */
'fallback_cb' => 'hale_main_nav_fallback',
'items_wrap' => '<ul id="%1$s" class="%2$s"><li><a href="'.home_url().'">Home</a></li>%3$s</ul>',
'walker' => new Bootstrap_Nav_Walker()/*,
'with_search' => true*/
)
);
<?php
$menu_at_primary_location = get_nav_menu_locations()['primary'];
$items = wp_get_nav_menu_items( $menu_at_primary_location );
if(empty($items)){
echo '<ul class="nav navbar-nav"><li><a href="'.home_url().'">Home</a></li></ul>';
} else {
$menu = wp_nav_menu(
array(
'menu_class' => 'nav navbar-nav'.$nav1.' '.$nav2,
'theme_location' => 'primary', /* where in the theme it's assigned */
'container' => false, /* container class */
'fallback_cb' => 'hale_main_nav_fallback',
'items_wrap' => '<ul id="%1$s" class="%2$s"><li><a href="'.home_url().'">Home</a></li>%3$s</ul>',
'walker' => new Bootstrap_Nav_Walker()/*,
'with_search' => true*/
)
);
}
?>
<div class="visible-xs-block">
<div class="navbar-form navbar-right">
Expand Down

0 comments on commit 0223f07

Please sign in to comment.