I’m trying to remove a specific link from my wordpress menu:
<li class="ubermenu-item ubermenu-item-type-post_type ubermenu-item-object-page ubermenu-item-1 ubermenu-item-level-0 ubermenu-column ubermenu-column-auto" ><a class="ubermenu-target ubermenu-item-layout-default ubermenu-item-layout-text_only" href="/first/" tabindex="0"><span class="ubermenu-target-title ubermenu-target-text">First</span></a></li>
<li class="ubermenu-item ubermenu-item-type-post_type ubermenu-item-object-page ubermenu-item-2 ubermenu-item-level-0 ubermenu-column ubermenu-column-auto" ><a class="ubermenu-target ubermenu-item-layout-default ubermenu-item-layout-text_only" href="/second/" tabindex="0"><span class="ubermenu-target-title ubermenu-target-text">Second</span></a></li>
<li class="ubermenu-item ubermenu-item-type-post_type ubermenu-item-object-page ubermenu-item-3 ubermenu-item-level-0 ubermenu-column ubermenu-column-auto" ><a class="ubermenu-target ubermenu-item-layout-default ubermenu-item-layout-text_only" href="/third/" tabindex="0"><span class="ubermenu-target-title ubermenu-target-text">Third</span></a></li>
function remove_link( $menu ) {
return str_replace( '<a href="', '<a>', $menu );
}
add_filter( 'wp_nav_menu', 'remove_link' );
The link that I’m trying to remove has the .ubermenu-item-3
attached to the li
element. How may I do this?
You should use the wp_nav_menu_objects filter instead. There’s no need to use potentially messy string replacement or DOM parsing because you can just filter the array of menu items before their markup is generated.
Here’s some skeleton code from http://hookr.io/filters/wp_nav_menu_objects/
Just unset the appropriate index in the array.
Or, simply use the UberMenu Conditionals Extension, which is designed for exactly that purpose. http://wpmegamenu.com/conditionals
Both versions below. read the comments! This depends on domdocument, most php installations will have it, but some custom set ups don’t.
Try this one if you’re excluding a page: