Advertisement
5ally

class-my-bs-walker-nav-menu

Mar 23rd, 2018
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.15 KB | None | 0 0
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3.     exit; // Exit if accessed directly.
  4. }
  5.  
  6. /**
  7.  * Custom walker for WordPress custom menus.
  8.  *
  9.  * @link https://wordpress.stackexchange.com/q/298663/137402
  10.  *
  11.  * @see Walker_Nav_Menu
  12.  */
  13. class My_BS_Walker_Nav_Menu extends Walker_Nav_Menu {
  14.     /**
  15.      * Starts the list before the elements are added.
  16.      *
  17.      * @see Walker_Nav_Menu::start_lvl()
  18.      */
  19.     public function start_lvl( &$output, $depth = 0, $args = array() ) {
  20.         // Default class.
  21.         $classes = array( 'menu-vertical' );
  22.  
  23.         /**
  24.          * Filters the CSS class(es) applied to a menu list element.
  25.          */
  26.         $class_names = join( ' ', apply_filters( 'nav_menu_submenu_css_class', $classes, $args, $depth ) );
  27.         $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
  28.  
  29.         $output .= '' .
  30.             '<div class="dropdown__container">' .
  31.                 '<div class="container">' .
  32.                     '<div class="row">' .
  33.                         '<div class="dropdown__content col-lg-2">' .
  34.                             "<ul{$class_names}>";
  35.     }
  36.  
  37.     /**
  38.      * Ends the list of after the elements are added.
  39.      *
  40.      * @see Walker_Nav_Menu::end_lvl()
  41.      */
  42.     public function end_lvl( &$output, $depth = 0, $args = array() ) {
  43.         $output .= '' .
  44.                             '</ul>' . // End .menu-vertical
  45.                         '</div>' .    // End .dropdown__content
  46.                     '</div>' .        // End .row
  47.                 '</div>' .            // End .container
  48.             '</div>';                 // End .dropdown__container
  49.     }
  50.  
  51.     /**
  52.      * Starts the element output.
  53.      *
  54.      * @see Walker_Nav_Menu::start_el()
  55.      */
  56.     public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
  57.         $item->item_spacing = 'discard';
  58.  
  59.         $classes = empty( $item->classes ) ? array() : (array) $item->classes;
  60.         if ( in_array( 'menu-item-has-children', $classes ) ) {
  61.             $depth2 = intval( $args->depth );
  62.             // Displays a link to the page and not a "dropdown trigger" text.
  63.             if ( $depth2 > 0 && (int) $depth + 1 === $depth2 ) {
  64.                 return parent::start_el( $output, $item, $depth, $args, $id );
  65.             }
  66.         } else {
  67.             // Displays a link to the page and not a "dropdown trigger" text.
  68.             return parent::start_el( $output, $item, $depth, $args, $id );
  69.         }
  70.  
  71.         $classes[] = 'dropdown';
  72.         $classes[] = 'menu-item-' . $item->ID;
  73.  
  74.         /**
  75.          * Filters the CSS class(es) applied to a menu item's list item element.
  76.          */
  77.         $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args, $depth ) );
  78.         $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
  79.  
  80.         $output .= '<li' . $class_names .'>';
  81.  
  82.         /** This filter is documented in wp-includes/post-template.php */
  83.         $title = apply_filters( 'the_title', $item->title, $item->ID );
  84.  
  85.         /**
  86.          * Filters a menu item's title.
  87.          */
  88.         $title = apply_filters( 'nav_menu_item_title', $title, $item, $args, $depth );
  89.  
  90.         $item_output = '<span class="dropdown__trigger">';
  91.         $item_output .= $title;
  92.         $item_output .= '</span>';
  93.  
  94.         $output .= $item_output;
  95.     }
  96.  
  97.     /**
  98.      * Ends the element output, if needed.
  99.      *
  100.      * @see Walker_Nav_Menu::end_el()
  101.      */
  102.     public function end_el( &$output, $item, $depth = 0, $args = array() ) {
  103.         $output .= '</li>';
  104.     }
  105.  
  106. } // My_BS_Walker_Nav_Menu
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement