How to Create a Responsive WordPress Menu Using Bootstrap Nav Walker
Are you looking for a way to start with a responsive navigation menu using Bootstrap? While there’s probably a plugin for this, we have created a quick code snippet that you can use to completely create a responsive WordPress menu using Bootstrap nav walker in WordPress.
Instructions:
First you need to download the wp_bootstrap_navwalker.php file from here and upload it to wp-content/themes/your-theme folder.
After that, you need to add this code to your theme’s functions.php file or in a site-specific plugin:
1
2
| // Register Custom Navigation Walker for function.php require_once ( 'wp_bootstrap_navwalker.php' );
|
Next, you need to create a Bootstrap menu and and add the following code to the header.php file of your theme.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
| // Create navigation <div class = "navbar " role= "navigation" > <div class = "navbar-header" > <button type= "button" class = "navbar-toggle" data-toggle= "collapse" data-target= ".navbar-collapse" > <span class = "sr-only" >Toggle navigation</span> <span class = "icon-bar" ></span> <span class = "icon-bar" ></span> <span class = "icon-bar" ></span> </button> </div> <div class = "navbar-collapse collapse " > <?php /* menu */ wp_nav_menu( array ( 'menu' => 'primary-menu' , 'theme_location' => 'primary-menu' , 'depth' => 5, '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()) ); ?> </div> </div><!-- Navigation --> ?> |
Make sure to replace the ‘menu’ parameter with the name of your Bootstrap menu.
Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly add code snippets in WordPress, so you don’t accidentally break your site.
If you liked this code snippet, please consider checking out our other articles on the site like: 50+ best responsive WordPress themes to create a mobile ready site and how to create a job application form in WordPress.
Post a Comment