引导程序导航栏在wordpress上不起作用


bootstrap navbar not works on wordpress

我在上个版本中通过引导程序制作了wordpress主题,但导航栏无法工作。我尝试了很多不同的方法,比如玩wp_nav_menu();代码和ulli的,但都没用!

这是我的header.php代码:

<!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
<title><?php bloginfo( 'title' ); ?></title>
  <meta charset="<?php bloginfo( 'charset' ); ?>" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta property="dc:creator" content="http://www.myvivaid.com/" />
    <meta name="geo.region" content="IR" />
    <meta name="googlebot" content="index,follow" />    
    <meta name="robots" content="noodp,noydir" />
    <meta name="robots" content="index, follow" />
    <link href="//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet">
    <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.1.0/css/font-awesome.min.css">
    <link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Raleway:400">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <!-- Bootstrap -->
    <link rel="stylesheet" href="<?php echo get_stylesheet_uri(); ?>" type="text/css" media="screen" />
<div class="col-12">
<nav class="navbar navbar-default">
  <div class="container-fluid">
    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav">
        <?php wp_nav_menu(); ?>
      </ul>
      <form class="navbar-form navbar-left" role="search">
        <div class="form-group">
          <input type="text" class="form-control" placeholder="Search">
        </div>
        <button type="submit" class="btn btn-default">Submit</button>
      </form>
    </div><!-- /.navbar-collapse -->
  </div><!-- /.container-fluid -->
</nav>
</div>
     <h1><a href="<?php bloginfo('url'); ?>"><?php bloginfo( $show ); ?></a></h1>
<!--end menu-->
<?php wp_enqueue_script("jquery"); ?>
<?php wp_head(); ?>
</head>
<body>

这是我的footer.php:

<div class="col-md-12" id="footer">
		<div class="col-lg-3">
			<div class="spacer-1">
				<a href="index.html"><img src="<?php bloginfo('template_directory'); ?>/img/myviva_footer.png" alt="مای ویوا آی دی"></a>
			</div>
			</div>
				<div class="col-lg-5">
				<div class="indent-top2">
					<p><span class="glyphicon glyphicon-copyright-mark" aria-hidden="true"></span> <a href="http://www.myvivaid.com/">طراحی قالب بصورت اختصاصی</a> توسط MyVivaId.com صورت گرفته و هرگونه کپی برداری از این طرح با نارضایتی <a href="http://webmaker.ga/">طراح</a> همراه می باشد.</p>
					<p><span class="glyphicon glyphicon-heart" aria-hidden="true"></span> <a href="http://webmaker.ga/">سفارش طراحی قالب</a><p>
				</div>
			</div>
			<div class="col-lg-4">
				<ul class="list-services">
					<li><a href="https://www.facebook.com/pages/Myvivaid/1593182370893821"><i class="fa fa-facebook-square fa-2x"></i></a></li>
					<li><a href="https://twitter.com/myvivaid"><i class="fa fa-twitter-square fa-2x"></i></a></li>
					<li><a href="https://plus.google.com/113198960466603112632"><i class="fa fa-google-plus-square fa-2x"></i></a></li>
					<li><a href="http://myvivaid.com/feed/"><i class="fa fa-rss-square fa-2x"></i></a></li>
				</ul>
				<span class="footer-text">&copy; 2015 <a class="link color-2" href="#">تمام حقوق محفوظ</a></span>
			</div>
			<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
			<script src="<?php echo esc_url( get_template_directory_uri() . '/js/bootstrap.min.js' ); ?>"></script>
			<?php wp_footer(); ?>
</div>
<a href="#top" id="smoothup" title="Back to top"></a>
</body>
</html>

您添加了wp_nav_menu函数类名、主题名称以及所有这些之后,它将在引导菜单上显示为类似的下拉菜单。

请参考此菜单功能名称并将其应用于您的代码中。

<?php wp_nav_menu( array( 'container_class' => 'menu-header', 'theme_location' => 'primary' ) ); ?>

按照本教程使用这个navwalker,您可以轻松地创建bootstrap meny。

wp引导navwalker

我希望这对你有用。

您可以尝试另一种方法,用几行代码代替wp_nav_menu()

我在我的一个wordpress主题中也做了同样的事情,即引导集成,你可以在那篇文章中找到详细的解释。

http://wpgenesistheme.com/wpblogs/integrating-menu-with-bootstrap-design/

我希望它对你有用。

感谢

将此代码添加到您的functions.php文件

       class Bootstrap_Walker_Nav_Menu extends Walker_Nav_Menu {
        function start_lvl( &$output, $depth ) {
            $indent = str_repeat( "'t", $depth );
            $output    .= "'n$indent<ul class='"dropdown-menu'">'n";
        }
        function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
            $indent = ( $depth ) ? str_repeat( "'t", $depth ) : '';
            $li_attributes = '';
            $class_names = $value = '';
            $classes = empty( $item->classes ) ? array() : (array) $item->classes;
            $classes[] = ($args->has_children) ? 'dropdown' : '';
            $classes[] = ($item->current || $item->current_item_ancestor) ? 'active' : '';
            $classes[] = 'menu-item-' . $item->ID;
            $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
            $class_names = ' class="' . esc_attr( $class_names ) . '"';
            $id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args );
            $id = strlen( $id ) ? ' id="' . esc_attr( $id ) . '"' : '';
            $output .= $indent . '<li' . $id . $value . $class_names . $li_attributes . '>';
            $attributes  = ! empty( $item->attr_title ) ? ' title="'  . esc_attr( $item->attr_title ) .'"' : '';
            $attributes .= ! empty( $item->target )     ? ' target="' . esc_attr( $item->target     ) .'"' : '';
            $attributes .= ! empty( $item->xfn )        ? ' rel="'    . esc_attr( $item->xfn        ) .'"' : '';
            $attributes .= ! empty( $item->url )        ? ' href="'   . esc_attr( $item->url        ) .'"' : '';
            $attributes .= ($args->has_children)        ? ' class="dropdown-toggle" data-toggle="dropdown"' : '';
            $item_output = $args->before;
            $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) ? '</a>' : '</a>';
            $item_output .= $args->after;
            $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
        }
        function display_element( $element, &$children_elements, $max_depth, $depth=0, $args, &$output ) {
            if ( !$element )
                return;
            $id_field = $this->db_fields['id'];
            //display this element
            if ( is_array( $args[0] ) ) 
                $args[0]['has_children'] = ! empty( $children_elements[$element->$id_field] );
            else if ( is_object( $args[0] ) ) 
                $args[0]->has_children = ! empty( $children_elements[$element->$id_field] ); 
            $cb_args = array_merge( array(&$output, $element, $depth), $args);
            call_user_func_array(array(&$this, 'start_el'), $cb_args);
            $id = $element->$id_field;
            // descend only when the depth is right and there are childrens for this element
            if ( ($max_depth == 0 || $max_depth > $depth+1 ) && isset( $children_elements[$id]) ) {
                foreach( $children_elements[ $id ] as $child ){
                    if ( !isset($newlevel) ) {
                        $newlevel = true;
                        //start the child delimiter
                        $cb_args = array_merge( array(&$output, $depth), $args);
                        call_user_func_array(array(&$this, 'start_lvl'), $cb_args);
                    }
                    $this->display_element( $child, $children_elements, $max_depth, $depth + 1, $args, $output );
                }
                    unset( $children_elements[ $id ] );
            }
            if ( isset($newlevel) && $newlevel ){
                //end the child delimiter
                $cb_args = array_merge( array(&$output, $depth), $args);
                call_user_func_array(array(&$this, 'end_lvl'), $cb_args);
            }
            //end this element
            $cb_args = array_merge( array(&$output, $element, $depth), $args);
            call_user_func_array(array(&$this, 'end_el'), $cb_args);
        }
    }

你应该这样调用导航菜单,在我的情况下,请使用你的主题位置,这是主菜单

 <nav class="navbar navbar-default" role="navigation">
                                    <div class="container-fluid">
                                        <!-- Collect the nav links, forms, and other content for toggling -->
                                        <div class="collapse navbar-collapse" id="navbar-collapse-1">
         <?php
            $args = array(
                 'theme_location'  => 'main-menu',
                'depth'      => 1,
                'container'  => false,
                'menu_class'     => 'nav navbar-nav navbar-right',
                'walker'     => new Bootstrap_Walker_Nav_Menu()
            );
                   wp_nav_menu($args);
          ?>
            <form class="navbar-form navbar-left" role="search">
                <div class="form-group">
               <input type="text" class="form-control" placeholder="Search">
                </div>
               <button type="submit" class="btn btn-default">Submit</button>
            </form>
                                        </div>
                                    </div>
                                </nav>