wp_nav_menu更改&添加子菜单& li>一个班级名字?

时间:2014-12-26 15:23:53

标签: wordpress menu submenu

我找到了很多改变“子菜单”的方法,但问题是我的朋友编写了一个新的Walker_Nav_Menu,这里是代码。

我想要的只是:

  1. 在第3行添加CLASS="ADD New HERE"
  2. 的课程
  3. 更改第4行中的课程,从<ul class="sub-menu">更改为<ul class="dropdown">
  4. 再次在第5行添加一个类,就像第3行一样。

    <ul id="menu-herid" class="nav navbar-nav" data-breakpoint="800">
    <li id="menu-item-2821" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children">
       <a CLASS="ADD New HERE" href="http://theme.dev/home/">Home</a>
          <ul class="sub-menu">
             <li id="menu-item-2838" class="menu-item menu-item-type-post_type menu-item-object-page">
                <a href="http://theme.dev/blog-with-slideshow/">Blog with Slideshow</a>
              </li>
             </ul>
    </li>
    </ul>
    

    //带描述的导航

    if (! class_exists('hs_description_walker')) {
            class hs_description_walker extends Walker_Nav_Menu {
                function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
                    global $wp_query;
                    $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
    
                    $class_names = $value = 'class="dropdown"';
    
                    $classes = empty( $item->classes ) ? array() : (array) $item->classes;
    
                    $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) );
                    $class_names = ' class="'. esc_attr( $class_names ) . '"';
    
                    $output .= $indent . '<li id="menu-item-'. $item->ID . '"' . $value . $class_names .'>';
    
                    $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        ) .'"' : '';
    
                    $description  = ! empty( $item->description ) ? '<span class="desc">'.esc_attr( $item->description ).'</span>' : '';
    
                    if($depth != 0) {
                        $description = $append = $prepend = "";
                    }
    
                    $item_output = $args->before;
                    $item_output .= '<a'. $attributes .'>';
                    $item_output .= $args->link_before;
    
                    if (isset($prepend))
                        $item_output .= $prepend;
    
                    $item_output .= apply_filters( 'the_title', $item->title, $item->ID );
    
                    if (isset($append))
                        $item_output .= $append;
    
                    $item_output .= $description.$args->link_after;
                    $item_output .= '</a>';
                    $item_output .= $args->after;
    
                    $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
                }
            }
        }
    
  5. 现在问题是我希望将子菜单更改为“”

2 个答案:

答案 0 :(得分:1)

$defaults = array(
    'theme_location'  => '',
    'menu'            => 'menu-name',
    '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'          => new My_Walker_Nav_Menu() //call walker 
);
//Add this in your theme functions.php file
class My_Walker_Nav_Menu extends Walker_Nav_Menu {
  function start_lvl(&$output, $depth) {
    $indent = str_repeat("\t", $depth);
    $output .= "\n$indent<ul class=\"dropdown\">\n";
  }
}

答案 1 :(得分:0)

你试试这段代码......

HTML DEMO

参考 link

Wordress代码

<nav class="navbar-collapse collapse photoshoot-menu">
     <?php wp_nav_menu(array('theme_location'  => 'primary','container' => ' ')); ?>
</nav>