Skip to content

Feat wordpress standard #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
# Bootstrap 5 WordPress navbar walker menu
[bootstrap-5-wordpress-navbar-walker](https://github.com/AlexWebLab/bootstrap-5-wordpress-navbar-walker)
## How to use:
1. Copy and paste the [bootstrap_5_wp_nav_menu_walker](https://github.com/AlexWebLab/bootstrap-5-wordpress-navbar-walker/blob/main/functions.php) class into the functions.php file of your theme;
1. Copy and paste the [Bootstrap_5_Wp_Nav_Menu_Walker](class-bootstrap-5-wp-nav-menu-walker.php) class into the functions.php file of your theme;

`OR`

Download file [class-bootstrap-5-wp-nav-menu-walker.php](class-bootstrap-5-wp-nav-menu-walker.php) and include this file into the functions.php file of your theme;

2. Register a new menu by adding the follow code into the functions.php file of your theme:
```php
register_nav_menu('main-menu', 'Main menu');
Expand All @@ -24,7 +29,7 @@ register_nav_menu('main-menu', 'Main menu');
'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()
'walker' => new Bootstrap_5_Wp_Nav_Menu_Walker()
));
?>
</div>
Expand Down
116 changes: 116 additions & 0 deletions class-bootstrap-5-wp-nav-menu-walker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php
/**
* Nav Menu Walker
*
* @package Bootstrap_5_Wp_Nav_Menu_Walker
*/

/**
* Bootstrap 5 wp_nav_menu walker
*
* Bootstrap 5 WordPress navigation menu walker
*/
class Bootstrap_5_Wp_Nav_Menu_Walker extends Walker_Nav_menu {
/**
* Current item
*
* @var string Current item
*/
private $current_item;

/**
* Dropdown menu
*
* @var array Dropdown menu alignment values.
*/
private $dropdown_menu_alignment_values = array(
'dropdown-menu-start',
'dropdown-menu-end',
'dropdown-menu-sm-strt',
'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',
);

/**
* Starts the list before the elements are added.
*
* @param string $output Used to append additional content (passed by reference).
* @param int $depth Depth of menu item. Used for padding.
* @param stdClass $args An object of wp_nav_menu() arguments.
*/
public 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, true ) ) {
$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";
}

/**
* Starts the element output.
*
* @param string $output Used to append additional content (passed by reference).
* @param WP_Post $item Menu item data object.
* @param int $depth Depth of menu item. Used for padding.
* @param stdClass $args An object of wp_nav_menu() arguments.
* @param int $id Optional. ID of the current menu item. Default 0.
*/
public function start_el( &$output, $item, $depth = 0, $args = null, $id = 0 ) {
$this->current_item = $item;

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

$li_attributes = '';

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

$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 );
}
}
79 changes: 0 additions & 79 deletions functions.php

This file was deleted.