Skip to content

Commit

Permalink
Updated fallback function to process menu args
Browse files Browse the repository at this point in the history
  • Loading branch information
PlanBrewski committed Sep 27, 2013
1 parent 45ee1b9 commit 0d7c5f4
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 43 deletions.
46 changes: 24 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,15 @@ Update your `wp_nav_menu()` function in `header.php` to use the new walker by ad
```php
<?php
wp_nav_menu( array(
'menu' => 'primary',
'theme_location' => 'primary',
'depth' => 2,
'container' => false,
'menu_class' => 'nav navbar-nav',
'fallback_cb' => 'wp_bootstrap_navwalker::fallback',
'walker' => new wp_bootstrap_navwalker())
);
'menu' => 'primary',
'theme_location' => 'primary',
'depth' => 2,
'container' => 'div',
'container_class' => 'collapse navbar-collapse navbar-ex1-collapse',
'menu_class' => 'nav navbar-nav',
'fallback_cb' => 'wp_bootstrap_navwalker::fallback',
'walker' => new wp_bootstrap_navwalker())
);
?>
```

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

<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse navbar-ex1-collapse">
<?php
wp_nav_menu( array(
'menu' => 'primary',
'theme_location' => 'primary',
'depth' => 2,
'container' => false,
'menu_class' => 'nav navbar-nav',
'fallback_cb' => 'wp_bootstrap_navwalker::fallback',
'walker' => new wp_bootstrap_navwalker())
);
?>
</div>
<?php
wp_nav_menu( array(
'menu' => 'primary',
'theme_location' => 'primary',
'depth' => 2,
'container' => 'div',
'container_class' => 'collapse navbar-collapse navbar-ex1-collapse',
'menu_class' => 'nav navbar-nav',
'fallback_cb' => 'wp_bootstrap_navwalker::fallback',
'walker' => new wp_bootstrap_navwalker())
);
?>
</div>
</nav>
```
Expand Down Expand Up @@ -135,6 +134,9 @@ To set a disabled link simoly set the **Title Attribute** to `disabled` and the

Changelog
------------
**2.0.4**
+ Updated fallback function to accept args array from wp_nav_menu

**2.0.3**
+ Included a fallback function that adds a link to the WordPress menu manager if no menu has been assigned to the theme location.

Expand Down
75 changes: 54 additions & 21 deletions wp_bootstrap_navwalker.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
/**
* Class Name: wp_bootstrap_navwalker
* GitHub URI: https://github.com/twittem/wp-bootstrap-navwalker
* 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.
* Version: 2.0.3
* 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.
* Version: 2.0.4
* Author: Edward McIntyre - @twittem
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*/

class wp_bootstrap_navwalker extends Walker_Nav_Menu {

/**
* @see Walker::start_lvl()
* @since 3.0.0
Expand Down Expand Up @@ -40,11 +40,7 @@ function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {

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

function display_element( $element, &$children_elements, $max_depth, $depth, $args, &$output ) {
if ( !$element ) {
return;
}
if ( !$element ) {
return;
}

$id_field = $this->db_fields['id'];
$id_field = $this->db_fields['id'];

//display this element
if ( is_object( $args[0] ) ) {
$args[0]->has_children = ! empty( $children_elements[$element->$id_field] );
}
//display this element
if ( is_object( $args[0] ) ) {
$args[0]->has_children = ! empty( $children_elements[$element->$id_field] );
}

parent::display_element($element, $children_elements, $max_depth, $depth, $args, $output);
}
parent::display_element($element, $children_elements, $max_depth, $depth, $args, $output);
}

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

function fallback() {
if( current_user_can( 'manage_options' ) ) {
echo '<li><a href="' . admin_url( 'nav-menus.php' ) . '">Add a menu</a></li>';
function fallback( $args ) {
if ( current_user_can( 'manage_options' ) ) {
$fb_output = null;

if ( $container ) {
$fb_output = '<' . $container;

if ( $container_id ) {
$fb_output .= ' id="' . $container_id . '"';
}

if ( $container_class ) {
$fb_output .= ' class="' . $container_class . '"';
}

$fb_output .= '>';
}

$fb_output .= '<ul';

if ( $menu_id ) {
$fb_output .= ' id="' . $menu_id . '"';
}

if ( $menu_class ) {
$fb_output .= ' class="' . $menu_class . '"';
}

$fb_output .= '>';
$fb_output .= '<li><a href="' . admin_url( 'nav-menus.php' ) . '">Add a menu</a></li>';
$fb_output .= '</ul>';

if ( $container ) {
$fb_output .= '</' . $container . '>';
}

echo $fb_output;
}
}
}

?>
?>

0 comments on commit 0d7c5f4

Please sign in to comment.