WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] wp_nav_menu add link class (2 posts)

  1. Damien
    Member
    Posted 3 years ago #

    Just wondering if anyone knows a way of adding a class to the first link displayed in wp_nav_menu, I've got it working so it displays a drop down like so

    <li id="menu-item-393" class="menu-item-393">
    <a href="/">Forums</a>
    <a href='/' class='SplitCtrl' rel='Menu'></a>
     <div class='Menu JsOnly tabMenu'>
     <div class='primaryContent menuHeader'>
     <h3>Forums</h3>
     <div class='muted'>Quick Thread/forum Links</div>
     </div>
     <ul class='secondarydroplinks'>
    <li id="menu-item-394" class="menu-item"><a href="/">child one</a></li>
    <li id="menu-item-396" class="menu-item"><a href="/">child two</a></li>
      </ul></div>
    </li>

    but is there any way I can give only the first generated a link a class called navlink? ie:

    <li id="menu-item-393" class="menu-item-393">
    <a href='/' class='navLink'>Forums</a>
    <a href='/' class='SplitCtrl' rel='Menu'></a>
    ...
  2. Damien
    Member
    Posted 3 years ago #

    solved with

    class Walker_xenpress extends Walker_Nav_Menu {
    function start_el(&$output, $item, $depth, $args) {
    global $wp_query;
    $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
    $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 ) );
    $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 .'>';
    $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        ) .'"' : '';
    $item_output = $args->before;
    $item_output .= '<a class="navLink"'. $attributes .'>';
    $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
    $item_output .= '</a>';
    $item_output .= $args->after;
    $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
    	}

    for now

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.