Skip to content

Commit b085fdb

Browse files
committed
v1.0.0
0 parents  commit b085fdb

File tree

3 files changed

+112
-0
lines changed

3 files changed

+112
-0
lines changed

.gitignore

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Compiled source
2+
*.com
3+
*.class
4+
*.dll
5+
*.exe
6+
*.o
7+
*.so
8+
9+
# Packages
10+
*.7z
11+
*.dmg
12+
*.gz
13+
*.iso
14+
*.jar
15+
*.rar
16+
*.tar
17+
*.zip
18+
19+
# Logs and databases
20+
*.log
21+
*.sql
22+
*.sqlite
23+
24+
# OS generated files
25+
.DS_Store
26+
.DS_Store?
27+
._*
28+
.Spotlight-V100
29+
.Trashes
30+
ehthumbs.db
31+
Thumbs.db

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Bootstrap 5 WordPress navbar walker
2+
[bootstrap-5-wordpress-navbar-walker](https://github.com/AlexWebLab/bootstrap-5-wordpress-navbar-walker)
3+
## How to use:
4+
5+
```html
6+
<nav class="navbar navbar-expand-md navbar-light bg-light">
7+
<div class="container-fluid">
8+
<a class="navbar-brand" href="#">Navbar</a>
9+
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#main-menu" aria-controls="main-menu" aria-expanded="false" aria-label="Toggle navigation">
10+
<span class="navbar-toggler-icon"></span>
11+
</button>
12+
13+
<div class="collapse navbar-collapse" id="main-menu">
14+
<?php
15+
wp_nav_menu(array(
16+
'theme_location' => 'main-menu',
17+
'container' => false,
18+
'menu_class' => '',
19+
'fallback_cb' => '__return_false',
20+
'items_wrap' => '<ul id="%1$s" class="navbar-nav me-auto mb-2 mb-md-0 %2$s">%3$s</ul>',
21+
'depth' => 2,
22+
'walker' => new bootstrap_5_wp_nav_menu_walker()
23+
));
24+
?>
25+
</div>
26+
</div>
27+
</nav>
28+
```

functions.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
// bootstrap 5 wp_nav_menu walker
3+
class bootstrap_5_wp_nav_menu_walker extends Walker_Nav_menu
4+
{
5+
function start_lvl(&$output, $depth = 0, $args = array())
6+
{
7+
$indent = str_repeat("\t", $depth);
8+
$submenu = ($depth > 0) ? ' sub-menu' : '';
9+
$output .= "\n$indent<ul class=\"dropdown-menu$submenu depth_$depth\">\n";
10+
}
11+
12+
function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0)
13+
{
14+
15+
$indent = ($depth) ? str_repeat("\t", $depth) : '';
16+
17+
$li_attributes = '';
18+
$class_names = $value = '';
19+
20+
$classes = empty($item->classes) ? array() : (array) $item->classes;
21+
22+
$classes[] = ($args->walker->has_children) ? 'dropdown' : '';
23+
$classes[] = ($item->current || $item->current_item_ancestor) ? 'active' : '';
24+
$classes[] = 'nav-item';
25+
$classes[] = 'nav-item-' . $item->ID;
26+
if ($depth && $args->walker->has_children) {
27+
$classes[] = 'dropdown-menu dropdown-menu-end';
28+
}
29+
30+
$class_names = join(' ', apply_filters('nav_menu_css_class', array_filter($classes), $item, $args));
31+
$class_names = ' class="' . esc_attr($class_names) . '"';
32+
33+
$id = apply_filters('nav_menu_item_id', 'menu-item-' . $item->ID, $item, $args);
34+
$id = strlen($id) ? ' id="' . esc_attr($id) . '"' : '';
35+
36+
$output .= $indent . '<li ' . $id . $value . $class_names . $li_attributes . '>';
37+
38+
$attributes = !empty($item->attr_title) ? ' title="' . esc_attr($item->attr_title) . '"' : '';
39+
$attributes .= !empty($item->target) ? ' target="' . esc_attr($item->target) . '"' : '';
40+
$attributes .= !empty($item->xfn) ? ' rel="' . esc_attr($item->xfn) . '"' : '';
41+
$attributes .= !empty($item->url) ? ' href="' . esc_attr($item->url) . '"' : '';
42+
43+
$attributes .= ($args->walker->has_children) ? ' class="nav-link dropdown-toggle" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false"' : ' class="nav-link"';
44+
45+
$item_output = $args->before;
46+
$item_output .= ($depth > 0) ? '<a class="dropdown-item"' . $attributes . '>' : '<a' . $attributes . '>';
47+
$item_output .= $args->link_before . apply_filters('the_title', $item->title, $item->ID) . $args->link_after;
48+
$item_output .= '</a>';
49+
$item_output .= $args->after;
50+
51+
$output .= apply_filters('walker_nav_menu_start_el', $item_output, $item, $depth, $args);
52+
}
53+
}

0 commit comments

Comments
 (0)