I don't know if i am asking this correctly, but i will try. I am doing this for history pushstate purpose.

Code to get menu wihout ul and li:

$topmenu= array(
  'container'       => false,
  'echo'            => false,
  'items_wrap'      => '%3$s',
  'depth'           => 0

echo strip_tags(wp_nav_menu( $topmenu), '<a>');

At the moment, it gives mes only <a> tag with href as expected.

For example:

<a href="http://example.com/page1">Page 1</a>
<a href="http://example.com/page2">Page 2</a>
<a href="http://example.com/page3">Page 3</a>
// etc

My question:

How to disable/hide full permalink/hyperlink in href attribute, but keep showing page slug?

What i need for example:

<a href="/page1"></a>
// etc

I tryed to make this work to changing nav-menu-template.php, but i failed.

Thanks for any answers.


First of all -

I tryed to make this work to changing nav-menu-template.php

Never edit a core file! This is always a bad idea. At best, you will lose your changes when core updates.

To do this, you can use the nav_menu_link_attributes filter to modify the href attribute. This filter will be applied to each menu item individually.

In this example, we hook a function to the filter and str_replace the home_url with an empty string. So if our home URL is http://example.com, and the URL is http://example.com/something, the new value of href will be /something. We then return the modified attributes.

function wpdev_remove_home_url_from_menu_items( $atts, $item, $args ) {
    $atts['href'] = str_replace( home_url(), '', $atts['href'] );
    return $atts;
add_filter( 'nav_menu_link_attributes', 'wpdev_remove_home_url_from_menu_items', 10, 3 );

We only used the $atts argument in this case, but you'll note that there are two other arguments- the menu item object and menu args, in case you need to do any checking of specific menu items or menus in order to decide how to modify the attributes.

You can also use this filter to add additional attributes, like data, rel, and target.

  • Thank you. This is only for pushstate purpose. I never needed to do such things in wordpres, but now i need.. i need for myself :D Thanks again. – Tauras Nov 1 '15 at 1:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.