Skip to content

Commit

Permalink
Added fallback function when no menu assigned
Browse files Browse the repository at this point in the history
  • Loading branch information
PlanBrewski committed Sep 25, 2013
1 parent 8eeba4d commit 45ee1b9
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 21 deletions.
24 changes: 14 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Update your `wp_nav_menu()` function in `header.php` to use the new walker by ad
'depth' => 2,
'container' => false,
'menu_class' => 'nav navbar-nav',
'fallback_cb' => 'wp_page_menu',
'fallback_cb' => 'wp_bootstrap_navwalker::fallback',
'walker' => new wp_bootstrap_navwalker())
);
?>
Expand All @@ -59,7 +59,7 @@ register_nav_menus( array(
Typically the menu is wrapped with additional markup, here is an example of a ` navbar-fixed-top` menu that collapse for responsive navigation.

```php
<nav class="navbar navbar-default navbar-fixed-top navbar-turquoise" role="navigation">
<nav class="navbar navbar-default navbar-fixed-top" role="navigation">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="container">
<div class="navbar-header">
Expand All @@ -69,12 +69,12 @@ Typically the menu is wrapped with additional markup, here is an example of a `
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>

<a class="navbar-brand" href="<?php bloginfo('url'); ?>">
<?php bloginfo('name'); ?>
</a>
</div>

<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse navbar-ex1-collapse">
<?php
Expand All @@ -84,22 +84,23 @@ Typically the menu is wrapped with additional markup, here is an example of a `
'depth' => 2,
'container' => false,
'menu_class' => 'nav navbar-nav',
'fallback_cb' => '',
'fallback_cb' => 'wp_bootstrap_navwalker::fallback',
'walker' => new wp_bootstrap_navwalker())
);
);
?>
</div><!-- /.navbar-collapse -->
</div><!-- /.container -->
</div>
</div>
</nav>
```

To change your menu style add Bootstrap nav class names to the `menu_class` declaration.

Review options in the Bootstrap docs for more information on nav classes
http://getbootstrap.com/components/#nav

Displaying the Menu
------------
To display the menu you must associate your menu with your theme location. You can do this by selecting your them location in the *Theme Locations* list wile editing your menu in the WordPress admin.
-------------------
To display the menu you must associate your menu with your theme location. You can do this by selecting your theme location in the *Theme Locations* list wile editing a menu in the WordPress menu manager.

Extras
------------
Expand Down Expand Up @@ -134,6 +135,9 @@ To set a disabled link simoly set the **Title Attribute** to `disabled` and the

Changelog
------------
**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.

**2.0.2**
+ Small tweak to ensure carets are only displayed on top level dropdowns.

Expand Down
41 changes: 30 additions & 11 deletions wp_bootstrap_navwalker.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* 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.2
* Version: 2.0.3
* Author: Edward McIntyre - @twittem
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
Expand Down Expand Up @@ -40,7 +40,11 @@ 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 @@ -143,19 +147,34 @@ 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
* =============
* If this function is assigned to the wp_nav_menu's fallback_cb variable
* 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.
*/

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

?>

0 comments on commit 45ee1b9

Please sign in to comment.