Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Nav_Menu_Div_Walker extends Walker_Nav_Menu {
- private $_submenu_buffer = '';
- private $_clear_buffer = false;
- function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
- global $wp_query;
- $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
- $class_names = $value = '';
- $classes = empty( $item->classes ) ? array() : (array) $item->classes;
- $classes[] = 'menu-item-' . $item->ID;
- $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
- $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
- $id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args );
- $id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
- $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 ) .'"' : '';
- $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;
- if ( $depth > 0 ) {
- $this->_submenu_buffer .= $indent . '<div' . $id . $value . $class_names .'>';
- $this->_submenu_buffer .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
- } else {
- $output .= $indent . '<div' . $id . $value . $class_names .'>';
- $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
- }
- }
- function end_el( &$output, $item, $depth = 0, $args = array() ) {
- if ( $depth > 0 ) {
- $this->_submenu_buffer .= "</div>\n";
- } else {
- $output .= "</div>\n";
- }
- if ( $this->_clear_buffer ) {
- $output .= $this->_submenu_buffer;
- $this->_submenu_buffer = '';
- }
- }
- function start_lvl(&$output, $depth, $item) {
- $indent = str_repeat("\t", $depth);
- if ( $depth >= 0 ) {
- $this->_submenu_buffer .= "\n$indent<div class=\"sub-menu\">\n";
- }
- }
- function end_lvl(&$output, $depth) {
- $indent = str_repeat("\t", $depth);
- $this->_submenu_buffer .= "$indent</div>\n";
- if ( $depth == 0 ) {
- $this->_submenu_buffer .= "</div>\n";
- $this->_clear_buffer = true;
- }
- }
- }
- // Example wp_nav_menu() call
- wp_nav_menu(array(
- 'theme_location' => 'main-menu',
- 'walker' => new Nav_Menu_Div_Walker(),
- 'items_wrap' => '<div id="%1$s" class="%2$s">%3$s</div>'
- )) ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement