wordpress进阶教程(三十九):wordpress输出bootstrap的菜单结构


现在自适应网页(即常说的响应式设计,一个网页在PC\平板\手机上显示不同的布局)用的越来越多,然而,对于大多数人来说,写一个自适应的网页并非易事,于是有了bootstrap。

Bootstrap是twitter的工程师利用业余时间制作推出的一个开源的用于前端开发的工具包,即里面已经写好了css js,你只需要引入它的js和css,然后根据要求,给网页的div添加相应的class属性,即可制作一个响应式网页。

说实话,bootstrap很方便,作者使用过一次,但是有一个缺点,那就是你需要引入bootstrap的框架的css和js,然而这个css里面,有很多代码都是你用不到的,这样就会产生很多冗余代码,而去除css的冗余代码绝对是个体力活,所以作者用过一次就不用了。

将bootstrap应用到wordpress上也很简单,唯一可能有困难的就是菜单,因为bootstrap的菜单有他自己的结构和属性,最新的菜单演示页面如下:http://v3.bootcss.com/examples/navbar/

bootcssmenu

查看网页源文件,可以看到菜单的结构大概是这样

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

要在wordpress上实现这个菜单结构看似不难,其实,里面Dropdown后面的<b class="caret"></b>对应网页中下拉菜单的那个到三角形,以及二级菜单的ul标签的class属性。除非你把菜单写死,否则使用wp_nav_menu函数是无法输出这两个内容的。

那要怎么办呢?
wordpress的wp_nav_menu函数参数如下:

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

其中的walker参数是关键。

更改你的主题菜单输出函数wp_nav_menu的walker参数:

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

上面代码中walker参数的值是一个类,所以接下来你需要添加这个类,在你的主题functions.php文件中添加下面代码:

或者https://github.com/twittem/wp-bootstrap-navwalker/blob/master/wp_bootstrap_navwalker.php

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