Since the question of how to add a Bootstrap 4, Sage-friendly navwalker comes up a lot in this forum, I made a Sage composer dependency to solve the problem. Now, all you need to do is include this composer package in your Sage theme and the navwalker will be available to use.
Check it out:
From the README:
Sets up a Bootstrap 4 Navwalker for Sage 9-based themes.
To install, run the following in your Sage9-based theme directory:
Dude, thank you so much for this.
Having to manage/upgrade the old one was becoming a real pain.
Edit: Actually, I have run into an issue.
After running composer require, when I attempt to use new wp_bootstrap4_navwalker()) as a walker, it gives me an error saying Class âwp_bootstrap4_navwalkerâ not found.
Sounds like youâre probably not in the right namespace. It looks like wp_bootstrap4_navwalker is in the App namespace, so does the file youâre calling it from have namespace App up at the top? Can you show/tell us some more context about where youâre instantiating the wp_bootstrap4_navwalker class?
If youâre not setting up your template data with Controller, youâll need to fully reference the \App\wp_bootstrap4_navwalker().
In your Blade file, probably header.blade.php
Fatal error: Class 'Walker_Nav_Menu' not found in ..\vendor\mwdelaney\sage-bootstrap4-navwalker\bootstrap4-navwalker.php on line 20
Script phpcs handling the test event returned with error code 255
Obviously the class exists within WordPress, but Composer doesnât know about it. How can this error be fixed?
âcomposer testâ returns your error. PHP Fatal error: Class 'Walker_Nav_Menu' not found in /srv/www/blog.xxx.test/current/web/app/themes/xxx/vendor/mwdelaney/sage-bootstrap4-navwalker/bootstrap4-navwalker.php on line 20
Itâs complaining because composer is running its tests with phpcs which is not able to load WordPress and therefor has no access to Walker_Nav_Menu. I donât know what tests youâre running, but if those tests arenât actually evaluating the output of the Bootstrap 4 nav walker, you might be able to prevent the error by just shiming in an empty object called Walker_Nav_Menu, i.e.
if (!class_exists('Walker_Nav_Menu')) {
class \Walker_Nav_Menu {}
}
Another one with the same error: âFatal error: Class âWalker_Nav_Menuâ not found in âŚroots/vendor/mwdelaney/sage-bootstrap4-navwalker/bootstrap4-navwalker.php on line 20â
This is the result after running âcomposer testâ from the theme directory:
phpcs
PHP Fatal error: Class âWalker_Nav_Menuâ not found in /Applications/AMPPS/www/roots/web/app/themes/astro/vendor/mwdelaney/sage-bootstrap4-navwalker/bootstrap4-navwalker.php on line 20
PHP Stack trace:
PHP 1. {main}() /Applications/AMPPS/www/roots/web/app/themes/astro/vendor/squizlabs/php_codesniffer/scripts/phpcs:0
PHP 2. include_once() /Applications/AMPPS/www/roots/web/app/themes/astro/vendor/squizlabs/php_codesniffer/scripts/phpcs:19
PHP 3. include_once() /Applications/AMPPS/www/roots/web/app/themes/astro/vendor/squizlabs/php_codesniffer/CodeSniffer/CLI.php:27
PHP 4. ComposerAutoloaderInit726026fe7d12aebd0d28444fd12d5cd0::getLoader() /Applications/AMPPS/www/roots/web/app/themes/astro/vendor/autoload.php:7
PHP 5. composerRequire726026fe7d12aebd0d28444fd12d5cd0() /Applications/AMPPS/www/roots/web/app/themes/astro/vendor/composer/autoload_real.php:56
PHP 6. require() /Applications/AMPPS/www/roots/web/app/themes/astro/vendor/composer/autoload_real.php:66
Fatal error: Class âWalker_Nav_Menuâ not found in /Applications/AMPPS/www/roots/web/app/themes/astro/vendor/mwdelaney/sage-bootstrap4-navwalker/bootstrap4-navwalker.php on line 20