[Support request] Change submenu with new walker

Home Forums Support Change submenu with new walker

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #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?

    Kind regards,

    Rogier

    GP Premium 1.4.3
    #416620
    Tom
    Lead Developer
    Lead Developer

    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;
    }
    #416924
    Rogier

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

    Kind regards,

    Rogier

    #417268
    Tom
    Lead Developer
    Lead Developer

    Ah, 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/

    #417346
    Rogier

    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 🙂

    Kind regards,

    Rogier

    #417570
    Tom
    Lead Developer
    Lead Developer

    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.

    #417602
    Rogier

    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!

    Kind regards,

    Rogier

    #417879
    Tom
    Lead Developer
    Lead Developer

    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.

Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.