Log1x / navi

A developer-friendly alternative to the WordPress NavWalker.
https://github.com/Log1x/navi
MIT License
315 stars 30 forks source link

Adding a menu item via a filter/dynamically #53

Closed astray-heavy-arms closed 2 years ago

astray-heavy-arms commented 2 years ago

I've created a menu item using a filter that injects a link after a certain link in my menu if I use a standard wp walker I can inject my button and it displays fine.

However I think the process maybe a little different for navi is there anyway to dynamically add an item in at X location in my menu array as I don't think standard wp filters work?

Log1x commented 2 years ago

What's an example of your filter? You'll more than likely want to throw a conditional in the navigation view like:

@if ($item->slug == 'example')
  <div>My custom item</div>
@endif
astray-heavy-arms commented 2 years ago
add_filter('wp_nav_menu_items', 'inject_custom_link');

function inject_custom_link($items, $args){

  if($args ->theme_location == 'main_navigation')
     $items .= 'my link code';

  return $items

}

This is very rough sudo code but yeah this is what I'm kinda trying to achieve. The above code if it targets my navi menu does nothing and won't display 'my link code' in the array that gets built but with a wp walker it will display that text/link.

Log1x commented 2 years ago

Filters do not work with Navi. You'll have to accomplish this with a conditional in your markup instead.

Feel free to continue replying in this issue if you need more help with what you're trying to accomplish. Perhaps non-sudo code would be better so I can get a better idea of what you're doing and help you from there.