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

Primary menu behavior #42

Closed
wants to merge 15 commits into from
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;
}
?>
13 changes: 8 additions & 5 deletions inc/nav-walker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
Expand Down
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
4 changes: 2 additions & 2 deletions js/admin-style.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ jQuery(document).ready(function($) {
if( !($(this['children'][0]).is(":visible")) ){
$(this).css('display', 'none');
}
});
});
}
});
});
132 changes: 66 additions & 66 deletions js/navigation.js
Original file line number Diff line number Diff line change
@@ -1,67 +1,67 @@
/**
* navigation.js
*
*/
jQuery(document).ready(function($) {
function replaceAnchors(selector) {
$(selector).each(function(){
var atag = $(this).children('a');
var newhref = getFirstLink(this);
atag.attr('href',newhref);
});
}
function getFirstLink(element){
var href = $(element).children('a').attr('href');
if(href == '#'){
var firstChild = $(element).children('ul').children('li').first();
if(firstChild.length == 0){
return href;
}
else {
return getFirstLink(firstChild);
}
}
else {
return href;
}
}
replaceAnchors('.menu-item-has-children');
});
/*
* Handles toggling the navigation menu for small screens.
*
( function() {
var container, button, menu;
container = document.getElementById( 'site-navigation' );
if ( ! container )
return;
button = container.getElementsByTagName( 'button' )[0];
if ( 'undefined' === typeof button )
return;
menu = container.getElementsByTagName( 'ul' )[0];
// Hide menu toggle button if menu is empty and return early.
if ( 'undefined' === typeof menu ) {
button.style.display = 'none';
return;
}
if ( -1 === menu.className.indexOf( 'nav-menu' ) )
menu.className += ' nav-menu';
button.onclick = function() {
if ( -1 !== container.className.indexOf( 'toggled' ) )
container.className = container.className.replace( ' toggled', '' );
else
container.className += ' toggled';
};
} )();
/**
* navigation.js
*
*/

jQuery(document).ready(function($) {

function replaceAnchors(selector) {
$(selector).each(function(){
var atag = $(this).children('a');
var newhref = getFirstLink(this);
atag.attr('href',newhref);
});
}
function getFirstLink(element){
var href = $(element).children('a').attr('href');
if(href == '#'){
var firstChild = $(element).children('ul').children('li').first();
if(firstChild.length == 0){
return href;
}
else {
return getFirstLink(firstChild);
}
}
else {
return href;
}
}

replaceAnchors('.menu-item-has-children');

});

/*
* Handles toggling the navigation menu for small screens.
*
( function() {
var container, button, menu;

container = document.getElementById( 'site-navigation' );
if ( ! container )
return;

button = container.getElementsByTagName( 'button' )[0];
if ( 'undefined' === typeof button )
return;

menu = container.getElementsByTagName( 'ul' )[0];

// Hide menu toggle button if menu is empty and return early.
if ( 'undefined' === typeof menu ) {
button.style.display = 'none';
return;
}

if ( -1 === menu.className.indexOf( 'nav-menu' ) )
menu.className += ' nav-menu';

button.onclick = function() {
if ( -1 !== container.className.indexOf( 'toggled' ) )
container.className = container.className.replace( ' toggled', '' );
else
container.className += ' toggled';
};
} )();
*/