- This topic has 7 replies, 2 voices, and was last updated 6 years ago by
Tom.
-
AuthorPosts
-
November 5, 2017 at 4:56 am #416499
Rogier
Hi Tom,
I’m trying to create a Mega Menu in my GeneratePress website with this tutorial: https://slicejack.com/create-fully-custom-wordpress-mega-menu-no-plugins-attached/
This is a great solution, because it offers to use Widget positions instead of (sub)menu items, so they can be more rich in context.
Now what I want to do, is change the primary menu bij replacing your Menu walker to:
<ul class="mega-menu"> <?php $locations = get_nav_menu_locations(); if ( isset( $locations[ 'mega_menu' ] ) ) { $menu = get_term( $locations[ 'mega_menu' ], 'nav_menu' ); if ( $items = wp_get_nav_menu_items( $menu->name ) ) { foreach ( $items as $item ) { echo "<li>"; echo "<a href=\"{$item->url}\">{$item->title}</a>"; if ( is_active_sidebar( 'mega-menu-widget-area-' . $item->ID ) ) { echo "<div id=\"mega-menu-{$item->ID}\" class=\"mega-menu\">"; dynamic_sidebar( 'mega-menu-widget-area-' . $item->ID ); echo "</div>"; } echo "</li>"; } } } ?> </ul>
I Found this old solution, but without success. https://gist.github.com/generatepress/47e6abb67b41883e1910
Maybe this old Gist doesn’t work anymore because of some updates of GeneratePress?
Could you help me point in the right direction with an new function how to change the Primary navigation?
November 5, 2017 at 10:10 am #416620Tom
Lead DeveloperLead DeveloperYou could probably simplify that a lot like this:
add_filter( 'wp_nav_menu_args', 'tu_primary_nav_walker' ); function tu_primary_nav_walker( $args ) { if ( 'primary' == $args['theme_location'] && class_exists( 'Your_New_Walker' ) ) { $args['walker'] = new Your_New_Walker(); } return $args; }
November 6, 2017 at 1:29 am #416924Rogier
Hi Tom, could you be more specific? I can’t seem to relate your suggestion with a working solution.
November 6, 2017 at 10:03 am #417268Tom
Lead DeveloperLead DeveloperAh, the gist you linked to was for custom walkers, but the tutorial you linked to doesn’t involve a custom walker.
Perhaps you could achieve something similar with these instructions: https://docs.generatepress.com/article/building-simple-mega-menu/
As well as a plugin like this: https://wordpress.org/plugins/widgets-in-menu/
November 6, 2017 at 11:54 am #417346Rogier
I did find your Mega Menu tutorial, but’s thats just a CSS fix.
Let me make my question more simple: How can I replace the GeneratePress Primary submenus list with a function to:
echo "<li>"; echo "<a href=\"{$item->url}\">{$item->title}</a>"; if ( is_active_sidebar( 'mega-menu-widget-area-' . $item->ID ) ) { echo "<div id=\"mega-menu-{$item->ID}\" class=\"mega-menu\">"; dynamic_sidebar( 'mega-menu-widget-area-' . $item->ID ); echo "</div>"; } echo "</li>";
Because I don’t want to use a plug-in when I don’t need to (that’s why I choose GeneratePress, it’s with developers in mind), I’ll ignore your last suggestion 🙂
November 6, 2017 at 11:08 pm #417570Tom
Lead DeveloperLead DeveloperThe way they’ve described it in that tutorial would mean completely removing the standard WordPress menu, and building it from scratch. More specifically, this part: https://gist.github.com/DomagojGojak/63011ced8bd09c3a34ea#file-header-php-L12-L31
So instead of allowing WordPress to build your menu, you’re building it yourself. It would be pretty complicated to mimic the GP menu exactly using this method.
The plugin I linked to is coded quite nicely, and looks like it accomplishes the same thing with more flexibility.
Rebuilding the GP menu is possible, but it would be a bit too much coding for me to provide here in the forums.
November 7, 2017 at 12:34 am #417602Rogier
I know generatePress offers an lot of filters to customize things. So what I understand now that there is no filter to replace the primary navigation or change the mark-up.
Thanks for your time!
November 7, 2017 at 8:41 am #417879Tom
Lead DeveloperLead DeveloperYou would have to build an entire new walker, or rebuild the menu itself using the method in the tutorial you linked to. Both are possible, but the code involved is quite extensive.
-
AuthorPosts
- You must be logged in to reply to this topic.