How to remove anchor of current menu item in navbar?

Question

WordPress nav menu created by wp_nav_menu can highlight current menu item. How can i strip anchor from current menu item?

For example, we have menu:

<ul>
 <li class="current-menu-item"><a href="/somelink">Home</a></li>
 <li><a href="/elselink">Item</a></li>
 ...
</ul>

Can we remove

<a href="/somelink">

and leave just “Home” if it is current page?

UPD.
In my functions.php I have a walker like this:

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-menu">n";
  }
}

and I’ve found the walker I need:

//Creating new walker class, which won't make current page linked.
class not_linked_cur_page_MenuWalker extends Walker_Nav_Menu
{
    function start_el(&$output, $item, $depth, $args)
    { //All code below until '$current_url'
      // is native Walker_Nav_Menu code. Then we compare requested URL and current URL.
      // If whey are equal - the text shows in <span> tag instead of <a>.
        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, $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 .'>';

        $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        ) .'"' : '';

        $current_url = (is_ssl()?'https://':'http://').$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
        $item_url = esc_attr( $item->url );

        if ($item_url == $current_url)
        {
            $item_output = $args->before;
            $item_output .= '<span'. $attributes .'>';
            $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
            $item_output .= '</span>';
            $item_output .= $args->after;
        }
        else
        {
            $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 .= '</a>';
            $item_output .= $args->after;
        }

        $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
    }
}

How I can make one walker of these two?

0
, Stas Ponomaryov 6 years 2015-09-24T09:05:45-05:00 0 Answers 87 views 0

Leave an answer

Browse
Browse