Convert Static Bootstrap Navigation to Dynamic WordPress Menu

Step 6 of Convert Bootstrap 4 Template to Wordpress Theme

This is the Step 6 of Tutorial How to Convert Bootstrap 4 Templates into WordPress Themes . In this step we will convert our static Bootstrap Navigation into a dynamic WordPress menu.

Let’s Begin

The current Bootstrap navigation in our static template is just below the <body> tag of your index.php file and the HTML code looks something like below.

If you paste this code in your WordPress theme header.php file under <body> tag, the navigation will look perfectly fine.

But since we want the ability to change the menu directly from our WordPress dashboard and to not edit the php files every time we make changes to our menu.

We will convert this menu into dynamic.

# 1 Modify Menu from WordPress Dashboard

Go to your wordpress dashboard.

Click on Appearance > Menus 

You will see a default Menu in this page. Remove “Sample Page” link from this menu. Start adding custom links to this Menu

  1. Link Test : Portfolio , URL : #portfolio
  2. Link Test : About , URL : #about
  3. Link Test : Contact , URL : #contact

Under the Menu Settings, Display Location -> Select Primary.

Click Save Menu

Wordpress dynamic menu

 

# 2 Add bs4navwalker.php 

bs4navwalker.php is a script that helps us to correctly define a dynamic menu in wordpress using wp_nav_menu function.

Grab the latest bs4navwaler.php from dupkey/bs4navwalker  . Save the bs4navwalker.php file in your theme root directory.

Open functions.php file in your editor. This file is located at the project root of the theme directory.

Add the following require function, just above the themename_setup() function.

 

# 3 Add Custom Menu

Next, let’s create a dynamic menu in our theme.

Open header.php file in your editor, and add the following code just below the <body> tag.

 

To explain the above code, wp_nav_menu will fetch the contents of menu we created on the wordpress dashboard. For this we need to provide some parameters to wp_nav_menu. Parameters determine which menu contents needs to be fetched,  and how the menu should be formed in HTML once the contents are fetched.

Most of the parameters of wp_nav_menu are self explanatory. If you are looking for more details for a parameter, visit the official documentation of wp_nav_menu

If you visit your website home page now, you should be able to see the menu. But it’s not exactly how is should look like.

For this you need to add classes to <li> and <a> tag of the menu. This cannot be achieved with wp_nav_menu function.

 

# 4 Add Custom Classes to List and Anchor tag of WordPress Menu.

To add custom class to list <li> tag elements of our wordpress menu.

Go to Appearance > Menus >

On the top of the page, you will see a Screen Options button, Click on that and check CSS classes checkbox.

Screen Options Css Classes WordPress

Once this option is checked, you should see a new text box in your menu items, where you can add your custom css class to the list elements.

Add nav-item mx-0 mx-lg-1 class for all your menu items.

 

To add custom class to anchor <a> tag elements of our wordpress menu.

Open your functions.php file , which is located in wordpress theme root directory and add following code at the bottom of the file.

Refresh your website’s home page, and you should see your Bootstrap Static Navigation turned into Dynamic WordPress Menu.

Now you can change this menu directly form wordpress dashboard, instead of editing html code.

Great , Let’s move to Step 7 : Remove the unwanted code from Underscores theme

 

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: