Advertisement
firoze

bootstrap-menu-dynamic-wp

Jul 29th, 2015
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.76 KB | None | 0 0
  1.  
  2.  
  3.  
  4. // functions.php
  5.  
  6. function mytheme_setup() {
  7.  
  8. register_nav_menus( array(
  9. 'bootstrap_menu' => __( 'Bootstrap Menu', 'Dropdown Nav Menu' ),
  10. ) );
  11.  
  12. }
  13. add_action( 'after_setup_theme', 'mytheme_setup' );
  14.  
  15.  
  16. <!-- ********************************************************************************************** -->
  17. // functions.php
  18.  
  19. /**
  20. * Class Name: wp_bootstrap_navwalker
  21. * GitHub URI: https://github.com/twittem/wp-bootstrap-navwalker
  22. * Description: A custom WordPress nav walker class to implement the Bootstrap 3 navigation style in a custom theme using the WordPress built in menu manager.
  23. * Version: 2.0.4
  24. * Author: Edward McIntyre - @twittem
  25. * License: GPL-2.0+
  26. * License URI: http://www.gnu.org/licenses/gpl-2.0.txt
  27. */
  28.  
  29. class wp_bootstrap_navwalker extends Walker_Nav_Menu {
  30.  
  31. /**
  32. * @see Walker::start_lvl()
  33. * @since 3.0.0
  34. *
  35. * @param string $output Passed by reference. Used to append additional content.
  36. * @param int $depth Depth of page. Used for padding.
  37. */
  38. public function start_lvl( &$output, $depth = 0, $args = array() ) {
  39. $indent = str_repeat( "\t", $depth );
  40. $output .= "\n$indent<ul role=\"menu\" class=\" dropdown-menu\">\n";
  41. }
  42.  
  43. /**
  44. * @see Walker::start_el()
  45. * @since 3.0.0
  46. *
  47. * @param string $output Passed by reference. Used to append additional content.
  48. * @param object $item Menu item data object.
  49. * @param int $depth Depth of menu item. Used for padding.
  50. * @param int $current_page Menu item ID.
  51. * @param object $args
  52. */
  53. public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
  54. $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
  55.  
  56. /**
  57. * Dividers, Headers or Disabled
  58. * =============================
  59. * Determine whether the item is a Divider, Header, Disabled or regular
  60. * menu item. To prevent errors we use the strcasecmp() function to so a
  61. * comparison that is not case sensitive. The strcasecmp() function returns
  62. * a 0 if the strings are equal.
  63. */
  64. if ( strcasecmp( $item->attr_title, 'divider' ) == 0 && $depth === 1 ) {
  65. $output .= $indent . '<li role="presentation" class="divider">';
  66. } else if ( strcasecmp( $item->title, 'divider') == 0 && $depth === 1 ) {
  67. $output .= $indent . '<li role="presentation" class="divider">';
  68. } else if ( strcasecmp( $item->attr_title, 'dropdown-header') == 0 && $depth === 1 ) {
  69. $output .= $indent . '<li role="presentation" class="dropdown-header">' . esc_attr( $item->title );
  70. } else if ( strcasecmp($item->attr_title, 'disabled' ) == 0 ) {
  71. $output .= $indent . '<li role="presentation" class="disabled"><a href="#">' . esc_attr( $item->title ) . '</a>';
  72. } else {
  73.  
  74. $class_names = $value = '';
  75.  
  76. $classes = empty( $item->classes ) ? array() : (array) $item->classes;
  77. $classes[] = 'menu-item-' . $item->ID;
  78.  
  79. $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
  80.  
  81. if ( $args->has_children )
  82. $class_names .= ' dropdown';
  83.  
  84. if ( in_array( 'current-menu-item', $classes ) )
  85. $class_names .= ' active';
  86.  
  87. $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
  88.  
  89. $id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args );
  90. $id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
  91.  
  92. $output .= $indent . '<li' . $id . $value . $class_names .'>';
  93.  
  94. $atts = array();
  95. $atts['title'] = ! empty( $item->title ) ? $item->title : '';
  96. $atts['target'] = ! empty( $item->target ) ? $item->target : '';
  97. $atts['rel'] = ! empty( $item->xfn ) ? $item->xfn : '';
  98.  
  99. // If item has_children add atts to a.
  100. if ( $args->has_children && $depth === 0 ) {
  101. $atts['href'] = '#';
  102. $atts['data-toggle'] = 'dropdown';
  103. $atts['class'] = 'dropdown-toggle';
  104. $atts['aria-haspopup'] = 'true';
  105. } else {
  106. $atts['href'] = ! empty( $item->url ) ? $item->url : '';
  107. }
  108.  
  109. $atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args );
  110.  
  111. $attributes = '';
  112. foreach ( $atts as $attr => $value ) {
  113. if ( ! empty( $value ) ) {
  114. $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
  115. $attributes .= ' ' . $attr . '="' . $value . '"';
  116. }
  117. }
  118.  
  119. $item_output = $args->before;
  120.  
  121. /*
  122. * Glyphicons
  123. * ===========
  124. * Since the the menu item is NOT a Divider or Header we check the see
  125. * if there is a value in the attr_title property. If the attr_title
  126. * property is NOT null we apply it as the class name for the glyphicon.
  127. */
  128. if ( ! empty( $item->attr_title ) )
  129. $item_output .= '<a'. $attributes .'><span class="glyphicon ' . esc_attr( $item->attr_title ) . '"></span>&nbsp;';
  130. else
  131. $item_output .= '<a'. $attributes .'>';
  132.  
  133. $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
  134. $item_output .= ( $args->has_children && 0 === $depth ) ? ' <span class="caret"></span></a>' : '</a>';
  135. $item_output .= $args->after;
  136.  
  137. $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
  138. }
  139. }
  140.  
  141. /**
  142. * Traverse elements to create list from elements.
  143. *
  144. * Display one element if the element doesn't have any children otherwise,
  145. * display the element and its children. Will only traverse up to the max
  146. * depth and no ignore elements under that depth.
  147. *
  148. * This method shouldn't be called directly, use the walk() method instead.
  149. *
  150. * @see Walker::start_el()
  151. * @since 2.5.0
  152. *
  153. * @param object $element Data object
  154. * @param array $children_elements List of elements to continue traversing.
  155. * @param int $max_depth Max depth to traverse.
  156. * @param int $depth Depth of current element.
  157. * @param array $args
  158. * @param string $output Passed by reference. Used to append additional content.
  159. * @return null Null on failure with no changes to parameters.
  160. */
  161. public function display_element( $element, &$children_elements, $max_depth, $depth, $args, &$output ) {
  162. if ( ! $element )
  163. return;
  164.  
  165. $id_field = $this->db_fields['id'];
  166.  
  167. // Display this element.
  168. if ( is_object( $args[0] ) )
  169. $args[0]->has_children = ! empty( $children_elements[ $element->$id_field ] );
  170.  
  171. parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output );
  172. }
  173.  
  174. /**
  175. * Menu Fallback
  176. * =============
  177. * If this function is assigned to the wp_nav_menu's fallback_cb variable
  178. * and a manu has not been assigned to the theme location in the WordPress
  179. * menu manager the function with display nothing to a non-logged in user,
  180. * and will add a link to the WordPress menu manager if logged in as an admin.
  181. *
  182. * @param array $args passed from the wp_nav_menu function.
  183. *
  184. */
  185. public static function fallback( $args ) {
  186. if ( current_user_can( 'manage_options' ) ) {
  187.  
  188. extract( $args );
  189.  
  190. $fb_output = null;
  191.  
  192. if ( $container ) {
  193. $fb_output = '<' . $container;
  194.  
  195. if ( $container_id )
  196. $fb_output .= ' id="' . $container_id . '"';
  197.  
  198. if ( $container_class )
  199. $fb_output .= ' class="' . $container_class . '"';
  200.  
  201. $fb_output .= '>';
  202. }
  203.  
  204. $fb_output .= '<ul';
  205.  
  206. if ( $menu_id )
  207. $fb_output .= ' id="' . $menu_id . '"';
  208.  
  209. if ( $menu_class )
  210. $fb_output .= ' class="' . $menu_class . '"';
  211.  
  212. $fb_output .= '>';
  213. $fb_output .= '<li><a href="' . admin_url( 'nav-menus.php' ) . '">Add a menu</a></li>';
  214. $fb_output .= '</ul>';
  215.  
  216. if ( $container )
  217. $fb_output .= '</' . $container . '>';
  218.  
  219. echo $fb_output;
  220. }
  221. }
  222. }
  223.  
  224.  
  225. <!-- ********************************************************************************************** -->
  226. // bootstrap nav menu
  227.  
  228. <!-- Static navbar -->
  229. <nav class="navbar navbar-default navbar-static-top my_container" role="navigation">
  230. <div class="container">
  231. <div class="row">
  232. <div class="col-md-12">
  233. <div class="navbar-header">
  234. <button style="width:100%;margin:8px;" type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
  235. <div class="pull-left">
  236. <span class=""><a class="text-uppercase pull-left" href="#">menu</a></span>
  237. </div>
  238. <div class="pull-right">
  239. <span class="icon-bar"></span>
  240. <span class="icon-bar"></span>
  241. <span class="icon-bar"></span>
  242. </div>
  243. </button>
  244.  
  245. </div>
  246. <div id="navbar" class="navbar-collapse collapse">
  247. <?php
  248. echo wp_nav_menu(
  249. array(
  250. 'theme_location' => 'bootstrap_menu',
  251. 'depth' => 2,
  252. 'container' => '',
  253. 'container_class' => 'collapse navbar-collapse ',
  254. 'container_id' => 'navbar',
  255. 'menu_class' => 'nav navbar-nav navbar-left', // to show menu in right add "navbar-right" to show menu in left add "navbar-left"
  256. 'fallback_cb' => 'wp_bootstrap_navwalker::fallback',
  257. 'walker' => new wp_bootstrap_navwalker())
  258. );
  259. ?>
  260. <style type="text/css">
  261. .navbar-toggle{ padding: 9px 35px !important;}
  262. .navbar-toggle{ padding: 9px 35px !important;}
  263. .navbar-nav{margin:0 0px !important;}
  264. .navbar, .navbar-header{margin-left:0px !important;margin-right:0px !important;border:0 !important}
  265. .navbar-toggle:hover{border:0 !important;background-color:# !important;color:#fff !important; }
  266. .navbar-toggle{border:0 !important; }
  267. .navbar-header{margin-left:0px !important;margin-right:0px !important;border:0 }
  268. #navbar{border:0 !important;}
  269. </style>
  270. </div><!--/.nav-collapse -->
  271. </div>
  272. </div>
  273. </div>
  274. </nav>
  275.  
  276.  
  277.  
  278.  
  279.  
  280. <!-- ********************************************************************************************** -->
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement