2

I'm building a theme on top of a starter theme (wp-bootstrap) and am having trouble getting the Nav Walker to show the current menu item. current-menu-item is not there when I use a web inspector on the output html. So therefore I can't style it.

This is the code in my template file:

          <?php
       /** Loading WordPress Custom Menu  **/
       wp_nav_menu( array(
          'menu'            => 'main-menu',
          'container' => '',
          'container_class' => 'navbar-blue',
          'menu_class'      => 'nav',
          'menu_id' => 'main-menu',
          'walker' => new Bootstrapwp_Walker_Nav_Menu()
      ) ); ?>

And this is the code in function.php:

include 'includes/class-bootstrapwp_walker_nav_menu.php';

And finally this is the code from the customer nav walker file:

class Bootstrapwp_Walker_Nav_Menu extends Walker_Nav_Menu {

function __construct() {
}
  function start_lvl(&$output, $depth) {

$indent = str_repeat("\t", $depth);
$output .= "\n$indent<ul class=\"dropdown-menu\">\n";
  }
}

I've looked over a few stackexchange answers and tried changing the code to match them, but nothing has solved the issue (ie: nothing has made current-menu-item appear in the html output).

Any help would be appreciated.

7
  • and you get current-menu-item when you skip the walker?
    – birgire
    Apr 2, 2013 at 12:54
  • I've deleted the walker line in the array and still no current-menu-item. I've also tried replacing the whole array block with the default version from the wp codex and no joy. Apr 3, 2013 at 9:04
  • Okay, I've discovered that if I place the wp-nav-menu array in header.php, I get current-menu-item to display but my site design has the same menu code in sidebar.php and it doesn't work - any suggestions on how to fix this? header.php was the original position for the menu in the starter theme framework. Apr 3, 2013 at 9:46
  • maybe you need to reset the global $post value again? you could try <?php wp_reset_postdata();?> before the sidebar part.
    – birgire
    Apr 3, 2013 at 10:21
  • Sadly no, that didn't work. Thank you for help on this by the way. Apr 3, 2013 at 10:47

1 Answer 1

0

Since it is working as you expect in the header.php file (but not in sidebar.php), you could add this into header.php:

global $my_nav;
$my_nav= wp_nav_menu( array(
          'menu'            => 'main-menu',
          'container' => '',
          'container_class' => 'navbar-blue',
          'menu_class'      => 'nav',
          'menu_id' => 'main-menu',
          'walker' => new Bootstrapwp_Walker_Nav_Menu(),
          'echo' => 0,
      ) ); ?>

and then in your sidebar.php file you could add

global $my_nav;
echo $my_nav;

to display the menu.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.