RUSSEL86

bootstrap menu dynamic

Aug 10th, 2014
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.80 KB | None | 0 0
  1. functions.php
  2. #............................................................................................
  3.  
  4. add_action( 'after_setup_theme', 'bootstrap_setup' );
  5.  
  6. if ( ! function_exists( 'bootstrap_setup' ) ):
  7.  
  8.         function bootstrap_setup(){
  9.  
  10.                 add_action( 'init', 'register_menu' );
  11.                        
  12.                 function register_menu(){
  13.                         register_nav_menu( 'top-bar', 'Bootstrap Top Menu' );
  14.                 }
  15.  
  16.                 class Bootstrap_Walker_Nav_Menu extends Walker_Nav_Menu {
  17.  
  18.                        
  19.                         function start_lvl( &$output, $depth ) {
  20.  
  21.                                 $indent = str_repeat( "\t", $depth );
  22.                                 $output    .= "\n$indent<ul class=\"dropdown-menu\">\n";
  23.                                
  24.                         }
  25.  
  26.                         function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
  27.                                
  28.                                 $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
  29.  
  30.                                 $li_attributes = '';
  31.                                 $class_names = $value = '';
  32.  
  33.                                 $classes = empty( $item->classes ) ? array() : (array) $item->classes;
  34.                                 $classes[] = ($args->has_children) ? 'dropdown' : '';
  35.                                 $classes[] = ($item->current || $item->current_item_ancestor) ? 'active' : '';
  36.                                 $classes[] = 'menu-item-' . $item->ID;
  37.  
  38.  
  39.                                 $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
  40.                                 $class_names = ' class="' . esc_attr( $class_names ) . '"';
  41.  
  42.                                 $id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args );
  43.                                 $id = strlen( $id ) ? ' id="' . esc_attr( $id ) . '"' : '';
  44.  
  45.                                 $output .= $indent . '<li' . $id . $value . $class_names . $li_attributes . '>';
  46.  
  47.                                 $attributes  = ! empty( $item->attr_title ) ? ' title="'  . esc_attr( $item->attr_title ) .'"' : '';
  48.                                 $attributes .= ! empty( $item->target )     ? ' target="' . esc_attr( $item->target     ) .'"' : '';
  49.                                 $attributes .= ! empty( $item->xfn )        ? ' rel="'    . esc_attr( $item->xfn        ) .'"' : '';
  50.                                 $attributes .= ! empty( $item->url )        ? ' href="'   . esc_attr( $item->url        ) .'"' : '';
  51.                                 $attributes .= ($args->has_children)        ? ' class="dropdown-toggle" data-toggle="dropdown"' : '';
  52.  
  53.                                 $item_output = $args->before;
  54.                                 $item_output .= '<a'. $attributes .'>';
  55.                                 $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
  56.                                 $item_output .= ($args->has_children) ? ' <b class="caret"></b></a>' : '</a>';
  57.                                 $item_output .= $args->after;
  58.  
  59.                                 $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
  60.                         }
  61.  
  62.                         function display_element( $element, &$children_elements, $max_depth, $depth=0, $args, &$output ) {
  63.                                
  64.                                 if ( !$element )
  65.                                         return;
  66.                                
  67.                                 $id_field = $this->db_fields['id'];
  68.  
  69.                                 //display this element
  70.                                 if ( is_array( $args[0] ) )
  71.                                         $args[0]['has_children'] = ! empty( $children_elements[$element->$id_field] );
  72.                                 else if ( is_object( $args[0] ) )
  73.                                         $args[0]->has_children = ! empty( $children_elements[$element->$id_field] );
  74.                                 $cb_args = array_merge( array(&$output, $element, $depth), $args);
  75.                                 call_user_func_array(array(&$this, 'start_el'), $cb_args);
  76.  
  77.                                 $id = $element->$id_field;
  78.  
  79.                                 // descend only when the depth is right and there are childrens for this element
  80.                                 if ( ($max_depth == 0 || $max_depth > $depth+1 ) && isset( $children_elements[$id]) ) {
  81.  
  82.                                         foreach( $children_elements[ $id ] as $child ){
  83.  
  84.                                                 if ( !isset($newlevel) ) {
  85.                                                         $newlevel = true;
  86.                                                         //start the child delimiter
  87.                                                         $cb_args = array_merge( array(&$output, $depth), $args);
  88.                                                         call_user_func_array(array(&$this, 'start_lvl'), $cb_args);
  89.                                                 }
  90.                                                 $this->display_element( $child, $children_elements, $max_depth, $depth + 1, $args, $output );
  91.                                         }
  92.                                                 unset( $children_elements[ $id ] );
  93.                                 }
  94.  
  95.                                 if ( isset($newlevel) && $newlevel ){
  96.                                         //end the child delimiter
  97.                                         $cb_args = array_merge( array(&$output, $depth), $args);
  98.                                         call_user_func_array(array(&$this, 'end_lvl'), $cb_args);
  99.                                 }
  100.  
  101.                                 //end this element
  102.                                 $cb_args = array_merge( array(&$output, $element, $depth), $args);
  103.                                 call_user_func_array(array(&$this, 'end_el'), $cb_args);
  104.                                
  105.                         }
  106.                        
  107.                 }
  108.  
  109.         }
  110.  
  111. endif;
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121. Header.php
  122. #............................................................................
  123.  
  124.                         <?php
  125.                                
  126.                                 $args = array(
  127.                                         'theme_location' => 'top-bar',
  128.                                         'depth'          => 2,
  129.                                         'container'      => false,
  130.                                         'menu_class'     => 'nav navbar-nav',
  131.                                         'walker'         => new Bootstrap_Walker_Nav_Menu()
  132.                                 );
  133.  
  134.                                 wp_nav_menu($args);
  135.                         ?>
  136.  
  137.  
  138. ===================   If Dropdown Has sub menu then  ========================
  139.  
  140. <?php
  141. $args = array(
  142. 'theme_location' => 'top-bar',
  143. 'depth'          => 3,
  144. 'container'      => false,
  145. 'menu_class'     => 'nav navbar-nav',
  146. 'walker'         => new Bootstrap_Walker_Nav_Menu()
  147. );
  148. wp_nav_menu($args);
  149. ?>
  150.  ================= add css =====================
  151. ul.dropdown-menu > li >.dropdown-menu {top: 0;left: 100%;}
  152. .open2 ul.dropdown-menu{display:block;}
  153. ==================== add jquery =====================
  154. jQuery(document).ready(function() {
  155.     jQuery(window).scroll(function(){
  156.         if(jQuery(this).scrollTop()>200){
  157.             jQuery('button#nana').addClass('fixed');
  158.         }
  159.         else{
  160.             jQuery('button#nana').removeClass('fixed');
  161.         }
  162.     });
  163.     jQuery('button#nana').click(function(){
  164.         jQuery('html body').animate({scrollTop:0},1000);
  165.     });
  166.    
  167. });
Advertisement
Add Comment
Please, Sign In to add comment