Advertisement
Guest User

Functions.php Code for Bootstrap Menu + Dropdown in WP

a guest
Aug 7th, 2013
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.05 KB | None | 0 0
  1. <?php // Twitter Bootstrap v3 Menu Adaptaion for WordPress
  2.  
  3. add_action( 'after_setup_theme', 'bootstrap_setup' );
  4.  
  5. if ( ! function_exists( 'bootstrap_setup' ) ):
  6.  
  7.     function bootstrap_setup(){
  8.  
  9.         add_action( 'init', 'register_menu' );
  10.            
  11.         function register_menu(){
  12.             register_nav_menu( 'top-bar', 'Bootstrap Top Menu' );
  13.         }
  14.  
  15.         class Bootstrap_Walker_Nav_Menu extends Walker_Nav_Menu {
  16.  
  17.            
  18.             function start_lvl( &$output, $depth ) {
  19.  
  20.                 $indent = str_repeat( "\t", $depth );
  21.                 $output    .= "\n$indent<ul class=\"dropdown-menu\">\n";
  22.                
  23.             }
  24.  
  25.             function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
  26.                
  27.                 $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
  28.  
  29.                 $li_attributes = '';
  30.                 $class_names = $value = '';
  31.  
  32.                 $classes = empty( $item->classes ) ? array() : (array) $item->classes;
  33.                 $classes[] = ($args->has_children) ? 'dropdown' : '';
  34.                 $classes[] = ($item->current || $item->current_item_ancestor) ? 'active' : '';
  35.                 $classes[] = 'menu-item-' . $item->ID;
  36.  
  37.  
  38.                 $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
  39.                 $class_names = ' class="' . esc_attr( $class_names ) . '"';
  40.  
  41.                 $id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args );
  42.                 $id = strlen( $id ) ? ' id="' . esc_attr( $id ) . '"' : '';
  43.  
  44.                 $output .= $indent . '<li' . $id . $value . $class_names . $li_attributes . '>';
  45.  
  46.                 $attributes  = ! empty( $item->attr_title ) ? ' title="'  . esc_attr( $item->attr_title ) .'"' : '';
  47.                 $attributes .= ! empty( $item->target )     ? ' target="' . esc_attr( $item->target     ) .'"' : '';
  48.                 $attributes .= ! empty( $item->xfn )        ? ' rel="'    . esc_attr( $item->xfn        ) .'"' : '';
  49.                 $attributes .= ! empty( $item->url )        ? ' href="'   . esc_attr( $item->url        ) .'"' : '';
  50.                 $attributes .= ($args->has_children)        ? ' class="dropdown-toggle" data-toggle="dropdown"' : '';
  51.  
  52.                 $item_output = $args->before;
  53.                 $item_output .= '<a'. $attributes .'>';
  54.                 $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
  55.                 $item_output .= ($args->has_children) ? ' <i class="icon-angle-down"></i> ' : '</a>';
  56.                 $item_output .= $args->after;
  57.  
  58.                 $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
  59.             }
  60.  
  61.             function display_element( $element, &$children_elements, $max_depth, $depth=0, $args, &$output ) {
  62.                
  63.                 if ( !$element )
  64.                     return;
  65.                
  66.                 $id_field = $this->db_fields['id'];
  67.  
  68.                 //display this element
  69.                 if ( is_array( $args[0] ) )
  70.                     $args[0]['has_children'] = ! empty( $children_elements[$element->$id_field] );
  71.                 else if ( is_object( $args[0] ) )
  72.                     $args[0]->has_children = ! empty( $children_elements[$element->$id_field] );
  73.                 $cb_args = array_merge( array(&$output, $element, $depth), $args);
  74.                 call_user_func_array(array(&$this, 'start_el'), $cb_args);
  75.  
  76.                 $id = $element->$id_field;
  77.  
  78.                 // descend only when the depth is right and there are childrens for this element
  79.                 if ( ($max_depth == 0 || $max_depth > $depth+1 ) && isset( $children_elements[$id]) ) {
  80.  
  81.                     foreach( $children_elements[ $id ] as $child ){
  82.  
  83.                         if ( !isset($newlevel) ) {
  84.                             $newlevel = true;
  85.                             //start the child delimiter
  86.                             $cb_args = array_merge( array(&$output, $depth), $args);
  87.                             call_user_func_array(array(&$this, 'start_lvl'), $cb_args);
  88.                         }
  89.                         $this->display_element( $child, $children_elements, $max_depth, $depth + 1, $args, $output );
  90.                     }
  91.                         unset( $children_elements[ $id ] );
  92.                 }
  93.  
  94.                 if ( isset($newlevel) && $newlevel ){
  95.                     //end the child delimiter
  96.                     $cb_args = array_merge( array(&$output, $depth), $args);
  97.                     call_user_func_array(array(&$this, 'end_lvl'), $cb_args);
  98.                 }
  99.  
  100.                 //end this element
  101.                 $cb_args = array_merge( array(&$output, $element, $depth), $args);
  102.                 call_user_func_array(array(&$this, 'end_el'), $cb_args);
  103.                
  104.             }
  105.            
  106.         }
  107.  
  108.     }
  109.  
  110. endif;
  111. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement