Controlling Nav Menu Generation

You might want to tweak the output of your menu items in WordPress. You can use this to remove or change a link from a specific item.

 

To do so we can make use of the filter  walker_nav_menu_start_el  – it’s called for each traversed menu item when building the menu. This is actually done in the  nav-menu-template.php script in the class  Walker_Nav_Menu and the function  start_el.
However this way you can hook into:

It will print out each item with attributes and it’s values.

 

We can make a real world use case and remove the link from a specific item:

This can be useful when you have several sub pages in your navigation but you don’t have any content in the parent page. The soulution is to make this page not accessible from the navigation.

 

Hopefully you find this filter as useful as I do. Please leave a comment describing how you use this hook.

If you have found a spelling error, please, notify us by selecting that text and pressing Ctrl+Enter.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Spelling error report

The following text will be sent to our editors: