現在自適應網頁 (即常説的響應式設計,一個網頁在 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/

查看網頁源文件,可以看到菜單的結構大概是這樣
- <ul class="nav navbar-nav">
- <li class="active"><a href="#">Link</a></li>
- <li class="dropdown">
- <a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown <b class="caret"></b></a>
- <ul class="dropdown-menu">
- <li><a href="#">Action</a></li>
- <li><a href="#">Another action</a></li>
- </ul>
- </li>
- </ul>
要在 WordPress 上實現這個菜單結構看似不難,其實,裏面 Dropdown 後面的<b ></b> 對應網頁中下拉菜單的那個到三角形,以及二級菜單的 ul 標籤的 class 屬性。除非你把菜單寫死,否則使用 wp_nav_menu 函數是無法輸出這兩個內容的。
那要怎麼辦呢?
WordPress 的 wp_nav_menu 函數參數如下:
- <?php
- $defaults = array(
- 'theme_location' => '',
- 'menu' => '',
- 'container' => 'div',
- 'container_class' => '',
- 'container_id' => '',
- 'menu_class' => 'menu',
- 'menu_id' => '',
- 'echo' => true,
- 'fallback_cb' => 'wp_page_menu',
- 'before' => '',
- 'after' => '',
- 'link_before' => '',
- 'link_after' => '',
- 'items_wrap' => '<ul id="%1$s" class="%2$s">%3$s</ul>',
- 'depth' => 0,
- 'walker' => ''
- );
- wp_nav_menu( $defaults );
- ?>
其中的 walker 參數是關鍵。
更改你的主題菜單輸出函數 wp_nav_menu 的 walker 參數:
- <?php
- wp_nav_menu( array(
- 'theme_location' => 'ashu_menu',
- 'depth' => 2,
- 'container' => false,
- 'menu_class' => 'nav navbar-nav',
- 'fallback_cb' => 'wp_page_menu',
- //添加或更改 walker 參數
- 'walker' => new wp_bootstrap_navwalker())
- );
- ?>
上面代碼中 walker 參數的值是一個類,所以接下來你需要添加這個類,在你的主題 functions.php 文件中添加下面代碼:
或者 https://github.com/twittem/wp-bootstrap-navwalker/blob/master/wp_bootstrap_navwalker.php
- <?php
- /**
- * Class Name: wp_bootstrap_navwalker
- * GitHub URI: https://github.com/twittem/wp-bootstrap-navwalker
- * 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.
- * Version: 2.0.4
- * Author: Edward McIntyre - @twittem
- * License: GPL-2.0+
- * License URI: http://www.gnu.org/licenses/gpl-2.0.txt
- */
- class wp_bootstrap_navwalker extends Walker_Nav_Menu {
- /**
- * @see Walker::start_lvl()
- * @since 3.0.0
- *
- * @param string $output Passed by reference. Used to append additional content.
- * @param int $depth Depth of page. Used for padding.
- */
- public function start_lvl( &$output, $depth = 0, $args = array() ) {
- $indent = str_repeat( " ", $depth );
- $output .= "
$indent<ul role="menu" class=" dropdown-menu">
"; - }
- /**
- * @see Walker::start_el()
- * @since 3.0.0
- *
- * @param string $output Passed by reference. Used to append additional content.
- * @param object $item Menu item data object.
- * @param int $depth Depth of menu item. Used for padding.
- * @param int $current_page Menu item ID.
- * @param object $args
- */
- public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
- $indent = ( $depth ) ? str_repeat( " ", $depth ) : '';
- /**
- * Dividers, Headers or Disabled
- * =============================
- * Determine whether the item is a Divider, Header, Disabled or regular
- * menu item. To prevent errors we use the strcasecmp() function to so a
- * comparison that is not case sensitive. The strcasecmp() function returns
- * a 0 if the strings are equal.
- */
- if ( strcasecmp( $item->attr_title, 'divider' ) == 0 && $depth === 1 ) {
- $output .= $indent . '<li role="presentation" class="divider">';
- } else if ( strcasecmp( $item->title, 'divider') == 0 && $depth === 1 ) {
- $output .= $indent . '<li role="presentation" class="divider">';
- } else if ( strcasecmp( $item->attr_title, 'dropdown-header') == 0 && $depth === 1 ) {
- $output .= $indent . '<li role="presentation" class="dropdown-header">' . esc_attr( $item->title );
- } else if ( strcasecmp($item->attr_title, 'disabled' ) == 0 ) {
- $output .= $indent . '<li role="presentation" class="disabled"><a href="#">' . esc_attr( $item->title ) . '</a>';
- } else {
- $class_names = $value = '';
- $classes = empty( $item->classes ) ? array() : (array) $item->classes;
- $classes[] = 'menu-item-' . $item->ID;
- $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
- if ( $args->has_children )
- $class_names .= ' dropdown';
- if ( in_array( 'current-menu-item', $classes ) )
- $class_names .= ' active';
- $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
- $id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args );
- $id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
- $output .= $indent . '<li' . $id . $value . $class_names .'>';
- $atts = array();
- $atts['title'] = ! empty( $item->title ) ? $item->title : '';
- $atts['target'] = ! empty( $item->target ) ? $item->target : '';
- $atts['rel'] = ! empty( $item->xfn ) ? $item->xfn : '';
- // If item has_children add atts to a.
- if ( $args->has_children && $depth === 0 ) {
- $atts['href'] = '#';
- $atts['data-toggle'] = 'dropdown';
- $atts['class'] = 'dropdown-toggle';
- $atts['aria-haspopup'] = 'true';
- } else {
- $atts['href'] = ! empty( $item->url ) ? $item->url : '';
- }
- $atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args );
- $attributes = '';
- foreach ( $atts as $attr => $value ) {
- if ( ! empty( $value ) ) {
- $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
- $attributes .= ' ' . $attr . '="' . $value . '"';
- }
- }
- $item_output = $args->before;
- /*
- * Glyphicons
- * ===========
- * Since the the menu item is NOT a Divider or Header we check the see
- * if there is a value in the attr_title property. If the attr_title
- * property is NOT null we apply it as the class name for the glyphicon.
- */
- if ( ! empty( $item->attr_title ) )
- $item_output .= '<a'. $attributes .'><span class="glyphicon ' . esc_attr( $item->attr_title ) . '"></span> ';
- else
- $item_output .= '<a'. $attributes .'>';
- $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
- $item_output .= ( $args->has_children && 0 === $depth ) ? ' <span class="caret"></span></a>' : '</a>';
- $item_output .= $args->after;
- $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
- }
- }
- /**
- * Traverse elements to create list from elements.
- *
- * Display one element if the element doesn't have any children otherwise,
- * display the element and its children. Will only traverse up to the max
- * depth and no ignore elements under that depth.
- *
- * This method shouldn't be called directly, use the walk() method instead.
- *
- * @see Walker::start_el()
- * @since 2.5.0
- *
- * @param object $element Data object
- * @param array $children_elements List of elements to continue traversing.
- * @param int $max_depth Max depth to traverse.
- * @param int $depth Depth of current element.
- * @param array $args
- * @param string $output Passed by reference. Used to append additional content.
- * @return null Null on failure with no changes to parameters.
- */
- public function display_element( $element, &$children_elements, $max_depth, $depth, $args, &$output ) {
- if ( ! $element )
- return;
- $id_field = $this->db_fields['id'];
- // Display this element.
- if ( is_object( $args[0] ) )
- $args[0]->has_children = ! empty( $children_elements[ $element->$id_field ] );
- parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output );
- }
- /**
- * Menu Fallback
- * =============
- * If this function is assigned to the wp_nav_menu's fallback_cb variable
- * and a manu has not been assigned to the theme location in the WordPress
- * menu manager the function with display nothing to a non-logged in user,
- * and will add a link to the WordPress menu manager if logged in as an admin.
- *
- * @param array $args passed from the wp_nav_menu function.
- *
- */
- public static function fallback( $args ) {
- if ( current_user_can( 'manage_options' ) ) {
- extract( $args );
- $fb_output = null;
- if ( $container ) {
- $fb_output = '<' . $container;
- if ( $container_id )
- $fb_output .= ' id="' . $container_id . '"';
- if ( $container_class )
- $fb_output .= ' class="' . $container_class . '"';
- $fb_output .= '>';
- }
- $fb_output .= '<ul';
- if ( $menu_id )
- $fb_output .= ' id="' . $menu_id . '"';
- if ( $menu_class )
- $fb_output .= ' class="' . $menu_class . '"';
- $fb_output .= '>';
- $fb_output .= '<li><a href="' . admin_url( 'nav-menus.php' ) . '">Add a menu</a></li>';
- $fb_output .= '</ul>';
- if ( $container )
- $fb_output .= '</' . $container . '>';
- echo $fb_output;
- }
- }
- }