A custom WordPress nav walker class to fully implement the Twitter Bootstrap 4.0+ navigation style (v3-branch available for Bootstrap 3) in a custom theme using the WordPress built in menu manager.
The WP Bootstrap Navwalker is a custom WordPress Nav Walker Class that allows users to implement the Bootstrap 4 navigation style in a custom WordPress theme using the built-in menu manager. It provides the correct syntax and CSS classes to utilize Bootstrap dropdown navigation.
To install the WP Bootstrap Navwalker, follow these steps:
class-wp-bootstrap-navwalker.php
file in your WordPress theme folder /wp-content/themes/your-theme/
functions.php
file /wp-content/themes/your-theme/functions.php
and add the following code:// Register Navwalker class alias
require_once get_template_directory() . '/class-wp-bootstrap-navwalker.php';
// Add theme support for the nav walker
register_nav_menus( array(
'primary' => __( 'Primary Menu', 'your-theme-text-domain' ),
) );
if ( ! file_exists( get_template_directory() . '/class-wp-bootstrap-navwalker.php' ) ) {
die( 'The Bootstrap Navwalker class file is missing.' );
}
functions.php
file:function your_theme_setup() {
register_nav_menu( 'primary', 'Primary Menu' );
}
add_action( 'after_setup_theme', 'your_theme_setup' );
wp_nav_menu()
functions in your theme to use the new walker by adding a 'walker'
item to the wp_nav_menu
args array:wp_nav_menu( array(
'menu_class' => 'navbar-nav',
'theme_location' => 'primary',
'depth' => 2,
'container' => 'div',
'container_class' => 'collapse navbar-collapse',
'container_id' => 'navbarNav',
'walker' => new wp_bootstrap_navwalker(),
) );
The WP Bootstrap Navwalker is a useful utility class for formatting WordPress theme menus with the correct syntax and CSS classes to implement Bootstrap dropdown navigation. It is compliant with Theme Review guidelines for wordpress.org theme submission and supports both Bootstrap 3 and Bootstrap 4. The installation process is well-documented, making it easy for developers to implement this custom Nav Walker in their WordPress themes.