如何在导航栏中移除当前菜单项的锚点?

时间:2015-09-24 作者:Stas Ponomaryov

由wp\\U nav\\U menu创建的Wordpress nav menu可以突出显示当前菜单项。如何从当前菜单项中删除锚定?

例如,我们有菜单:

<ul>
 <li class="current-menu-item"><a href="/somelink">Home</a></li>
 <li><a href="/elselink">Item</a></li>
 ...
</ul>
我们可以删除吗

<a href="/somelink">
如果是当前页面,只留下“主页”吗?

UPD。在我的功能中。php我有这样一个助行器:

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";
  }
}
我找到了我需要的助行器:

//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 );
    }
}
我怎么能把这两个人变成一个步行者呢?

2 个回复
SO网友:Dunimas

米歇尔是对的。您可以创建自己的walker并重写创建锚的默认方法。

或者,您可以通过以下方式进行破解:

jQuery(document).ready(function($) {

    var current =  $("#nav_menu_id").find(".current-menu-item");
    $(current).html( $(current).find("a").html() );

});
其中,“nav\\u menu\\u id”是您希望在其上完成此操作的菜单的id。

就我个人而言,我会把时间花在步行机上。

SO网友:Abdul Rahuman M
class not_linked_cur_page_MenuWalker extends Walker_Nav_Menu {

    function start_lvl(&$output, $depth = 0, $args = array()) {

        $level = $depth + 1;
        $indent = str_repeat("\\t", $depth);
        $output .= "\\n$indent<ul class=\\"sub-menu sub-menu-level-$level\\" role=\\"menu\\" aria-hidden=\\"true\\">\\n";

    }

    function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0) {

        $default_classes = empty($item->classes) ? array () : (array) $item->classes;

        $custom_classes = (array)get_post_meta($item->ID, \'_menu_item_classes\', true);

        // Global class for all items
        $custom_classes[] = \'menu-item\';

        // Now with 100% more accessibility!
        $aria_attr[] = \'role="menuitem"\';

        // Is this a top-level menu item?
        if ($depth == 0)
            $custom_classes[] = \'menu-item-top-level\';

        // Does this menu item have children?
        if (in_array(\'menu-item-has-children\', $default_classes))
            $custom_classes[] = \'menu-item-has-children\';
            $aria_attr[] = \'aria-haspopup="true"\';

        // Is this menu item active? (Top level only)
        $active_classes = array(\'current-menu-item\', \'current-menu-parent\', \'current-menu-ancestor\', \'current_page_item\', \'current-page-parent\', \'current-page-ancestor\');
        if ($depth == 0 && array_intersect($default_classes, $active_classes))
            $custom_classes[] = \'menu-item-active\';

        // Give menu item a class based on its level/depth
        $level = $depth + 1;
        if ($depth > 0)
            $custom_classes[] = "menu-item-level-$level";

        $classes = join(\' \', $custom_classes);
        $aria = join(\' \', $aria_attr);
        !empty($classes)
            and $classes = \' class="\'. trim(esc_attr($classes)) . \'"\';

        $output .= "<li $classes $aria>";

        $attributes  = \'\';

        !empty($item->attr_title)
            and $attributes .= \' title="\'  . esc_attr($item->attr_title) .\'"\';
        !empty($item->target)
            and $attributes .= \' target="\' . esc_attr($item->target    ) .\'"\';
        !empty($item->xfn)
            and $attributes .= \' rel="\'    . esc_attr($item->xfn       ) .\'"\';
        !empty($item->url)
            and $attributes .= \' href="\'   . esc_attr($item->url       ) .\'"\';

        $title = apply_filters(\'the_title\', $item->title, $item->ID);
         if (in_array(\'menu-item-active\', $custom_classes)){
                $item_output = $args->before
                    . $args->link_before
                    . $title
                    . $args->after;
         }
         else{
            $item_output = $args->before
                . "<a class=\'menu-item-link\' $attributes>"
                . $args->link_before
                . $title
                . \'</a> \'
                . $args->link_after
                . $args->after;
         }

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

        // Add arrow icon if this item has children
        if (in_array(\'menu-item-has-children\', $default_classes)) {
            $output .= "<button class=\'sub-menu-toggle\' aria-label=\'Toggle Sub-menu Item\' aria-expanded=\'false\'>";
            $output .= "<i class=\'down-arrow\'></i>";
            $output .= "</button>";
        }
    }
}

相关推荐

Wordpress URL error for links

我的数据库中有以下内容siteurl subdomain.example.com home subdomain.example.com 当我尝试转到某些页面时,页面会转到subdomain.example.com/services/subdomain.example.com这有什么原因吗?由于这个奇怪的错误,我也无法进入wp管理,我不明白为什么会发生这种情况?我尝试用更改数据库中的值http:// 也已经预先准备好了。如有任何建议,我们将不胜感激。