Site logo

Archived topic

Change submenu with new walker

7 replies · Started by Rogier on November 5, 2017

Viewing posts 1–8 of 8

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?

You 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;
}

Hi Tom, could you be more specific? I can't seem to relate your suggestion with a working solution.

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 :)

The 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.

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!

You 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.

This archived topic is closed to new replies.