Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- if ( ! defined( 'ABSPATH' ) ) {
- exit; // Exit if accessed directly.
- }
- /**
- * Custom walker for WordPress custom menus.
- *
- * @link https://wordpress.stackexchange.com/q/298663/137402
- *
- * @see Walker_Nav_Menu
- */
- class My_BS_Walker_Nav_Menu extends Walker_Nav_Menu {
- /**
- * Starts the list before the elements are added.
- *
- * @see Walker_Nav_Menu::start_lvl()
- */
- public function start_lvl( &$output, $depth = 0, $args = array() ) {
- // Default class.
- $classes = array( 'menu-vertical' );
- /**
- * Filters the CSS class(es) applied to a menu list element.
- */
- $class_names = join( ' ', apply_filters( 'nav_menu_submenu_css_class', $classes, $args, $depth ) );
- $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
- $output .= '' .
- '<div class="dropdown__container">' .
- '<div class="container">' .
- '<div class="row">' .
- '<div class="dropdown__content col-lg-2">' .
- "<ul{$class_names}>";
- }
- /**
- * Ends the list of after the elements are added.
- *
- * @see Walker_Nav_Menu::end_lvl()
- */
- public function end_lvl( &$output, $depth = 0, $args = array() ) {
- $output .= '' .
- '</ul>' . // End .menu-vertical
- '</div>' . // End .dropdown__content
- '</div>' . // End .row
- '</div>' . // End .container
- '</div>'; // End .dropdown__container
- }
- /**
- * Starts the element output.
- *
- * @see Walker_Nav_Menu::start_el()
- */
- public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
- $item->item_spacing = 'discard';
- $classes = empty( $item->classes ) ? array() : (array) $item->classes;
- if ( in_array( 'menu-item-has-children', $classes ) ) {
- $depth2 = intval( $args->depth );
- // Displays a link to the page and not a "dropdown trigger" text.
- if ( $depth2 > 0 && (int) $depth + 1 === $depth2 ) {
- return parent::start_el( $output, $item, $depth, $args, $id );
- }
- } else {
- // Displays a link to the page and not a "dropdown trigger" text.
- return parent::start_el( $output, $item, $depth, $args, $id );
- }
- $classes[] = 'dropdown';
- $classes[] = 'menu-item-' . $item->ID;
- /**
- * Filters the CSS class(es) applied to a menu item's list item element.
- */
- $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args, $depth ) );
- $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
- $output .= '<li' . $class_names .'>';
- /** This filter is documented in wp-includes/post-template.php */
- $title = apply_filters( 'the_title', $item->title, $item->ID );
- /**
- * Filters a menu item's title.
- */
- $title = apply_filters( 'nav_menu_item_title', $title, $item, $args, $depth );
- $item_output = '<span class="dropdown__trigger">';
- $item_output .= $title;
- $item_output .= '</span>';
- $output .= $item_output;
- }
- /**
- * Ends the element output, if needed.
- *
- * @see Walker_Nav_Menu::end_el()
- */
- public function end_el( &$output, $item, $depth = 0, $args = array() ) {
- $output .= '</li>';
- }
- } // My_BS_Walker_Nav_Menu
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement