適用於Bootstrap的菜單自定義類

Bootstrap是Twitter推出的開源前端框架(CSS、js、圖標字體,該有的都有了),響應式設計,操作簡單,上手容易,但是要寫出優秀的網頁,那就得多實踐、多學習了!順便說一下,Bootstrap比阿里巴巴推出的Kissy(js庫)要好使多了!

本文傳播一個適用於Wordpress的Bootstrap菜單開源類!

先上代碼:

  1. <?php  
  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 = emptyempty$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  = ! emptyempty$item->attr_title ) ? ' title="'  . esc_attr( $item->attr_title ) .'"' : '';  
  47.                 $attributes .= ! emptyempty$item->target )     ? ' target="' . esc_attr( $item->target     ) .'"' : '';  
  48.                 $attributes .= ! emptyempty$item->xfn )        ? ' rel="'    . esc_attr( $item->xfn        ) .'"' : '';  
  49.                 $attributes .= ! emptyempty$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) ? ' <b class="caret"></b></a>' : '</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'] = ! emptyempty$children_elements[$element->$id_field] );  
  71.                 else if ( is_object$args[0] ) )   
  72.                     $args[0]->has_children = ! emptyempty$children_elements[$element->$id_field] );   
  73.                 $cb_args = array_mergearray(&$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_mergearray(&$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_mergearray(&$output$depth), $args);  
  97.                     call_user_func_array(array(&$this, 'end_lvl'), $cb_args);  
  98.                 }  
  99.    
  100.                 //end this element  
  101.                 $cb_args = array_mergearray(&$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. ?>  
上面的代碼複製到functions.php中,使用很簡單,示例

  1. <div class="navbar navbar-fixed-top">  
  2.     <div class="navbar-inner">  
  3.         <div class="container">  
  4.             <?php  
  5.                   
  6.                 $args = array(  
  7.                     'theme_location' => 'top-bar',  
  8.                     'depth'      => 2,  
  9.                     'container'  => false,  
  10.                     'menu_class'     => 'nav',  
  11.                     'walker'     => new Bootstrap_Walker_Nav_Menu()  
  12.                 );  
  13.    
  14.                 wp_nav_menu($args);  
  15.               
  16.             ?>  
  17.         </div>  
  18.     </div>  
  19. </div> 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章