Advertisement
firoze

bootstrap menu usages in wordpress

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