Not working when menu does not exists
I want to show the pages if there is no menu selected but I get some errors starting at line 48. Notice: Trying to get property of non-object in C:\xampp\htdocs\wp-content\themes\the_theme\inc\twitter_bootstrap_nav_walker.class.php on line 48
I'm using WP 3.5.1
I'm not sure I understand how your getting an error but from what I understand it is throwing an error when you have no menu defined. This is actually quite simple to solve.
The wp_nav_menu function is WordPress provides a fallback we can use. To do this you need to add 'fallback_cb' => 'wp_page_menu' to the wp_nav_menu array.
The menu declaration in your theme should look something like this:
<?php
wp_nav_menu( array(
'menu' => 'top_menu',
'depth' => 2,
'container' => false,
'menu_class' => 'nav',
//fallback_cb is required when no menus is present.
'fallback_cb' => 'wp_page_menu',
'walker' => new twitter_bootstrap_nav_walker())
);
?>
I've done it but it didn't worked.
Can you try to provide a more detailed explanation of how your menus are set up. I have tested this version of the script from version WordPress 3.2 through 3.6 RC2 with no issues so far.
It is interesting to me that the error starts at Line 48 as that section of code deals with sub menus. Can you copy your wp_nav_menu function an provide it here?
Sorry to open this thread again, but I have the same issue Wordpress 3.7.1 just installed, so no menu is set except the default that comes with Wordpress (I guess it's the one created by wp_page_menu() ) With the following code:
wp_nav_menu( array(
'theme_location' => 'primary',
'container' => 'div',
'container_class' => 'nav-collapse collapse',
'walker' => new wp_bootstrap_navwalker(),
) );
And even adding:
'fallback_cb' => 'wp_page_menu',
I get a bunch of errors:
Notice: Trying to get property of non-object in D:\Web\wp\wp-content\themes\iwstudio\assets\inc\wp_bootstrap_navwalker.php on line 68
Notice: Trying to get property of non-object in D:\Web\wp\wp-content\themes\iwstudio\assets\inc\wp_bootstrap_navwalker.php on line 86
Notice: Trying to get property of non-object in D:\Web\wp\wp-content\themes\iwstudio\assets\inc\wp_bootstrap_navwalker.php on line 105
Notice: Trying to get property of non-object in D:\Web\wp\wp-content\themes\iwstudio\assets\inc\wp_bootstrap_navwalker.php on line 121
Notice: Trying to get property of non-object in D:\Web\wp\wp-content\themes\iwstudio\assets\inc\wp_bootstrap_navwalker.php on line 121
Notice: Trying to get property of non-object in D:\Web\wp\wp-content\themes\iwstudio\assets\inc\wp_bootstrap_navwalker.php on line 123
Notice: Trying to get property of non-object in D:\Web\wp\wp-content\themes\iwstudio\assets\inc\wp_bootstrap_navwalker.php on line 124
Please, can you give us some clue? Thanks
No problem,
The issue is the default fallback is the wp_page_menu class. When wp_page_menu is called it takes the variables defined in your wp_nav_menu array and tries to process them, which is exactly what we would expect.
Unfortunately, wp_page_menu does not support all of the variables that wp_nav_menu does and the most important thing that is not processed is custom walkers. As on WordPress 3.6 you are required to explicitly set a menu to the theme location, to get around this the class includes a custom fallback function.
When the fallback is defined a regular visitor will see no output, and an logged in admin user will see one menu item linking then to the menu manager to define a menu. Here is the full menu declaration from the ReadMe file, not the difference in the fallback_cb declaration.
<?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())
);
?>
That said, you are going to have issues either way. I see by your CSS classes in your example that you are using Bootstrap 2.x and this class is structured for Bootstrap 3.x
You can find the legacy walker for Bootstrap 2.x here: https://github.com/twittem/wp-bootstrap-navwalker/tree/For-Bootstrap-2.3.2
Thanks for the answer, twittem! I'll stick with a fallback that echoes something like "Hey, you should create a custom menu to see it in action" :-)