Let’s say you want to add content to your navigation without prepending it or appending it to the end (which is much easier). In trying to build a mega menu that relies on a widget area and hooks in at a specific nav location, I ran into some issues using WordPress’s native Nav_Menu_Walker and found little documentation about it.
You may run into a few of the same issues, so here’s how I troubleshot it:
1. your widget outputs not as intended but instead ends up above or outside your navigation html
this is because you are not output buffering your widget – essentially output buffering allows the widget content to be grabbed preliminarily and then outputted. without output buffering, the widget content may not get outputted at all or get outputted in an unintended place
2. your widget outputs but doesn’t look as intended
this is because you didn’t properly format or wrap your widget. by default it is outputted in a series of div tags, if you are trying to add it to your menu to make a mega menu, then you should wrap your widget in respective html tags, for example, if you are outputting the widget within
tags because you want it to be a sub-navigation item, you need to wrap it in a list tag. even though you can technically put divs inside an ordered or unordered list tag (ol) or (ul), its not recommended – by keeping any divs like widget area content in list item (li) tags, it won’t only help when you go to style with css, but the browser won’t have to “guess” what you’re trying to do because its valid html.
3. you get a “1” in the dom when outputting your widget
If you are new to output buffering ( ob_start() … ob_get_clean(); ), you might expect to do something like:
$nav_widget = genesis_widget_area( 'nav-mega-menu-widget' );
$item_output .= $nav_widget; // results in a "1" being added in the dom
$item_output .= ob_get_clean();
but by adding $nav_widget after declaring it ($item_output .= $nav_widget; ), you end up with that 1 being added. I have no idea why but that’s the culprit. It likely has something to do with a boolean output, where it gets added, and it’s telling you “1” for true, while adding it without it being explicitly added to $item_output.
Full Code here
Leave a Reply