Skip to content

Commit 0d7c5f4

Browse files
committed
Updated fallback function to process menu args
1 parent 45ee1b9 commit 0d7c5f4

File tree

2 files changed

+78
-43
lines changed

2 files changed

+78
-43
lines changed

README.md

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,15 @@ Update your `wp_nav_menu()` function in `header.php` to use the new walker by ad
3535
```php
3636
<?php
3737
wp_nav_menu( array(
38-
'menu' => 'primary',
39-
'theme_location' => 'primary',
40-
'depth' => 2,
41-
'container' => false,
42-
'menu_class' => 'nav navbar-nav',
43-
'fallback_cb' => 'wp_bootstrap_navwalker::fallback',
44-
'walker' => new wp_bootstrap_navwalker())
45-
);
38+
'menu' => 'primary',
39+
'theme_location' => 'primary',
40+
'depth' => 2,
41+
'container' => 'div',
42+
'container_class' => 'collapse navbar-collapse navbar-ex1-collapse',
43+
'menu_class' => 'nav navbar-nav',
44+
'fallback_cb' => 'wp_bootstrap_navwalker::fallback',
45+
'walker' => new wp_bootstrap_navwalker())
46+
);
4647
?>
4748
```
4849

@@ -75,20 +76,18 @@ Typically the menu is wrapped with additional markup, here is an example of a `
7576
</a>
7677
</div>
7778

78-
<!-- Collect the nav links, forms, and other content for toggling -->
79-
<div class="collapse navbar-collapse navbar-ex1-collapse">
80-
<?php
81-
wp_nav_menu( array(
82-
'menu' => 'primary',
83-
'theme_location' => 'primary',
84-
'depth' => 2,
85-
'container' => false,
86-
'menu_class' => 'nav navbar-nav',
87-
'fallback_cb' => 'wp_bootstrap_navwalker::fallback',
88-
'walker' => new wp_bootstrap_navwalker())
89-
);
90-
?>
91-
</div>
79+
<?php
80+
wp_nav_menu( array(
81+
'menu' => 'primary',
82+
'theme_location' => 'primary',
83+
'depth' => 2,
84+
'container' => 'div',
85+
'container_class' => 'collapse navbar-collapse navbar-ex1-collapse',
86+
'menu_class' => 'nav navbar-nav',
87+
'fallback_cb' => 'wp_bootstrap_navwalker::fallback',
88+
'walker' => new wp_bootstrap_navwalker())
89+
);
90+
?>
9291
</div>
9392
</nav>
9493
```
@@ -135,6 +134,9 @@ To set a disabled link simoly set the **Title Attribute** to `disabled` and the
135134

136135
Changelog
137136
------------
137+
**2.0.4**
138+
+ Updated fallback function to accept args array from wp_nav_menu
139+
138140
**2.0.3**
139141
+ Included a fallback function that adds a link to the WordPress menu manager if no menu has been assigned to the theme location.
140142

wp_bootstrap_navwalker.php

Lines changed: 54 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
/**
44
* Class Name: wp_bootstrap_navwalker
55
* GitHub URI: https://github.com/twittem/wp-bootstrap-navwalker
6-
* Description: A custom WordPress nav walker class to implement the Twitter Bootstrap 2.3.2 navigation style in a custom theme using the WordPress built in menu manager.
7-
* Version: 2.0.3
6+
* Description: A custom WordPress nav walker class to implement the Bootstrap 3 navigation style in a custom theme using the WordPress built in menu manager.
7+
* Version: 2.0.4
88
* Author: Edward McIntyre - @twittem
99
* License: GPL-2.0+
1010
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
1111
*/
1212

1313
class wp_bootstrap_navwalker extends Walker_Nav_Menu {
14-
14+
1515
/**
1616
* @see Walker::start_lvl()
1717
* @since 3.0.0
@@ -40,11 +40,7 @@ function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
4040

4141
/**
4242
* Dividers, Headers or Disabled
43-
<<<<<<< HEAD
44-
* =============================
45-
=======
4643
* =============================
47-
>>>>>>> Added a fallback function when no menu assigned
4844
* Determine whether the item is a Divider, Header, Disabled or regular
4945
* menu item. To prevent errors we use the strcasecmp() function to so a
5046
* comparison that is not case sensitive. The strcasecmp() function returns
@@ -147,19 +143,19 @@ function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
147143
*/
148144

149145
function display_element( $element, &$children_elements, $max_depth, $depth, $args, &$output ) {
150-
if ( !$element ) {
151-
return;
152-
}
146+
if ( !$element ) {
147+
return;
148+
}
153149

154-
$id_field = $this->db_fields['id'];
150+
$id_field = $this->db_fields['id'];
155151

156-
//display this element
157-
if ( is_object( $args[0] ) ) {
158-
$args[0]->has_children = ! empty( $children_elements[$element->$id_field] );
159-
}
152+
//display this element
153+
if ( is_object( $args[0] ) ) {
154+
$args[0]->has_children = ! empty( $children_elements[$element->$id_field] );
155+
}
160156

161-
parent::display_element($element, $children_elements, $max_depth, $depth, $args, $output);
162-
}
157+
parent::display_element($element, $children_elements, $max_depth, $depth, $args, $output);
158+
}
163159

164160
/**
165161
* Menu Fallback
@@ -168,13 +164,50 @@ function display_element( $element, &$children_elements, $max_depth, $depth, $ar
168164
* and a manu has not been assigned to the theme location in the WordPress
169165
* menu manager the function with display nothing to a non-logged in user,
170166
* and will add a link to the WordPress menu manager if logged in as an admin.
167+
*
168+
* @param array $args passed from the wp_nav_menu function
169+
*
171170
*/
172171

173-
function fallback() {
174-
if( current_user_can( 'manage_options' ) ) {
175-
echo '<li><a href="' . admin_url( 'nav-menus.php' ) . '">Add a menu</a></li>';
172+
function fallback( $args ) {
173+
if ( current_user_can( 'manage_options' ) ) {
174+
$fb_output = null;
175+
176+
if ( $container ) {
177+
$fb_output = '<' . $container;
178+
179+
if ( $container_id ) {
180+
$fb_output .= ' id="' . $container_id . '"';
181+
}
182+
183+
if ( $container_class ) {
184+
$fb_output .= ' class="' . $container_class . '"';
185+
}
186+
187+
$fb_output .= '>';
188+
}
189+
190+
$fb_output .= '<ul';
191+
192+
if ( $menu_id ) {
193+
$fb_output .= ' id="' . $menu_id . '"';
194+
}
195+
196+
if ( $menu_class ) {
197+
$fb_output .= ' class="' . $menu_class . '"';
198+
}
199+
200+
$fb_output .= '>';
201+
$fb_output .= '<li><a href="' . admin_url( 'nav-menus.php' ) . '">Add a menu</a></li>';
202+
$fb_output .= '</ul>';
203+
204+
if ( $container ) {
205+
$fb_output .= '</' . $container . '>';
206+
}
207+
208+
echo $fb_output;
176209
}
177210
}
178211
}
179212

180-
?>
213+
?>

0 commit comments

Comments
 (0)