Niraj Kashyap
Niraj Kashyap

Reputation: 1

wp_nav_menu change & add sub-menu & li>a class name?

I found lots of methods to change the "sub-menu", but the problem is my friend coded a new Walker_Nav_Menu and here is the code.

All I want is:

  1. to add a class in 3rd line where CLASS="ADD New HERE"
  2. to change the class in 4th line, from <ul class="sub-menu"> to <ul class="dropdown">
  3. again add a class in the 5th line, just like the 3rd line.

    <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>
    

    // Navigation with description

    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 );
                }
            }
        }
    

Now the thing is I wanted the sub-menu to be changed to ""

Upvotes: 0

Views: 4912

Answers (2)

Mansukh Khandhar
Mansukh Khandhar

Reputation: 2582

you try to this code...

HTML DEMO

Reference link

Wordress Code

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

Upvotes: 0

gaurav kumar
gaurav kumar

Reputation: 1091

$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";
  }
}

Upvotes: 1

Related Questions