Create A WordPress Navigation Menu With Bootstrap 5

How To Create A WordPress Navigation Menu With Bootstrap 5

Do you want to create a navbar with Bootstrap 5 in WordPress. We will show how to create navbar in your WordPress themes. Navbar is also called a WordPress navigation menu. That’s a horizontal list of links displayed on top of most websites. 

To set up a navbar with bootstrap 5 in WordPress the first time you need to create a manu location . Then we can setup fronted. Do you know how to create menu locations or register a manu location in WordPress? 

Register A Menu Location  In WordPress:

We can use WordPress’ default function named register_nav_menu() . To register a navigation menu location for a theme. This function has two-parameter, 

  • $Location: Menu location identifier
  • $Description: Menu location descriptive text

Let’s see the code:

Use this code on functions.php

function wpk_bootstrap_menu() {
  register_nav_menu('bootstrap-menu',__( 'Bootstrap 5 Menu' ));
}
add_action( 'init', 'wpk_bootstrap_menu' );

Now you can go Appearance » Menus of your WordPress admin panel.  And You can see a new menu location created named “Bootstrap 5 Menu”.

Register A Menu Location  In WordPress
New Menu Location Created

Bootstrap WordPress Nav Walker Class:

WordPress uses a special class, called the Walker class, designed to assist traverse and display elements having a hierarchical data structure. WordPress goes through menu pages to display the things employing a walker object. The function Walker_Nav_Menu class is located in wp-includes/navmenu-template.

Code Sources

<?php
// bootstrap 5 wp_nav_menu walker
class bootstrap_5_wp_nav_menu_walker extends Walker_Nav_menu
{
  private $current_item;
  private $dropdown_menu_alignment_values = [
    'dropdown-menu-start',
    'dropdown-menu-end',
    'dropdown-menu-sm-start',
    'dropdown-menu-sm-end',
    'dropdown-menu-md-start',
    'dropdown-menu-md-end',
    'dropdown-menu-lg-start',
    'dropdown-menu-lg-end',
    'dropdown-menu-xl-start',
    'dropdown-menu-xl-end',
    'dropdown-menu-xxl-start',
    'dropdown-menu-xxl-end'
  ];

  function start_lvl(&$output, $depth = 0, $args = null)
  {
    $dropdown_menu_class[] = '';
    foreach($this->current_item->classes as $class) {
      if(in_array($class, $this->dropdown_menu_alignment_values)) {
        $dropdown_menu_class[] = $class;
      }
    }
    $indent = str_repeat("\t", $depth);
    $submenu = ($depth > 0) ? ' sub-menu' : '';
    $output .= "\n$indent<ul class=\"dropdown-menu$submenu " . esc_attr(implode(" ",$dropdown_menu_class)) . " depth_$depth\">\n";
  }

  function start_el(&$output, $item, $depth = 0, $args = null, $id = 0)
  {
    $this->current_item = $item;

    $indent = ($depth) ? str_repeat("\t", $depth) : '';

    $li_attributes = '';
    $class_names = $value = '';

    $classes = empty($item->classes) ? array() : (array) $item->classes;

    $classes[] = ($args->walker->has_children) ? 'dropdown' : '';
    $classes[] = 'nav-item';
    $classes[] = 'nav-item-' . $item->ID;
    if ($depth && $args->walker->has_children) {
      $classes[] = 'dropdown-menu dropdown-menu-end';
    }

    $class_names =  join(' ', apply_filters('nav_menu_css_class', array_filter($classes), $item, $args));
    $class_names = ' class="' . esc_attr($class_names) . '"';

    $id = apply_filters('nav_menu_item_id', 'menu-item-' . $item->ID, $item, $args);
    $id = strlen($id) ? ' id="' . esc_attr($id) . '"' : '';

    $output .= $indent . '<li ' . $id . $value . $class_names . $li_attributes . '>';

    $attributes = !empty($item->attr_title) ? ' title="' . esc_attr($item->attr_title) . '"' : '';
    $attributes .= !empty($item->target) ? ' target="' . esc_attr($item->target) . '"' : '';
    $attributes .= !empty($item->xfn) ? ' rel="' . esc_attr($item->xfn) . '"' : '';
    $attributes .= !empty($item->url) ? ' href="' . esc_attr($item->url) . '"' : '';

    $active_class = ($item->current || $item->current_item_ancestor || in_array("current_page_parent", $item->classes, true) || in_array("current-post-ancestor", $item->classes, true)) ? 'active' : '';
    $nav_link_class = ( $depth > 0 ) ? 'dropdown-item ' : 'nav-link ';
    $attributes .= ( $args->walker->has_children ) ? ' class="'. $nav_link_class . $active_class . ' dropdown-toggle" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false"' : ' class="'. $nav_link_class . $active_class . '"';

    $item_output = $args->before;
    $item_output .= '<a' . $attributes . '>';
    $item_output .= $args->link_before . apply_filters('the_title', $item->title, $item->ID) . $args->link_after;
    $item_output .= '</a>';
    $item_output .= $args->after;

    $output .= apply_filters('walker_nav_menu_start_el', $item_output, $item, $depth, $args);
  }
}

Include this class in your theme. In the next section, we will set up the fronted in your header.php.

Setup a menu on the header:

Navigation-Menus are customizable menus in your theme. they permit users to feature Pages, Posts, Categories, and URLs to the menu. to make a navigation menu you’ll got to register it, then display the menu within the appropriate location in your theme.

Edit header.php

<div class="collapse navbar-collapse" id="navbarSupportedContent">
    	<?php
            wp_nav_menu(array(
                'theme_location' => 'bootstrap-menu',
                'container' => false,
                'menu_class' => '',
                'fallback_cb' => '__return_false',
                'items_wrap' => '<ul id="%1$s" class="navbar-nav me-auto mb-2 mb-md-0 %2$s">%3$s</ul>',
                'depth' => 2,
                'walker' => new bootstrap_5_wp_nav_menu_walker()
            ));
            ?>
   
    </div>

That’s it.

Conclusion:

We hope, This article will help you. If you face any problem with your WordPress navigation menu feel free to contact us. We will help you. Please comment below.

See More:

How to Add Image Icons With Navigation Menus in WordPress Without Plugin

Add Button in WordPress Navigation Menu

Create an Advanced Theme Options Panel in WordPress

5 comments

  1. F*ckin?amazing things here. I抦 very glad to see your post. Thanks a lot and i’m looking forward to contact you. Will you kindly drop me a mail?

Leave a Reply

Your email address will not be published.