Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

Hook area in default mobile menu?

  • I prefer the look of the default mobile menu (where the logo and hamburger/close are visible above the mobile drop down menu).
    Are there any hooks available for the default mobile menu? I would like to add a button after the menu items.
    I know I can easily do this using elements and hooks with the off-canvas menu (overlay and/or slide-in from side).
    But I can’t see a way of adding a button to a hook in the default menu.

    If there are no hooks available in the default mobile menu, is there a way to make the off-canvas menu look like the default one? I am trying to get the mobile header to stay visible when the hamburger is clicked, the menu be visible below it, and have the menu close on click of the hamburger when it changes to a close button.

    I realise this might now be 2 questions, for which I apologise. If the answer to my original question is “No, there are no hooks in the default mobile menu” I will submit the second question separately.

    Thank you for any assistance. Generatepress rocks!

  • Hi there,

    there are some ways to do it.
    But first, is it a “real” button or would making a menu item look like a button suffice ?

  • Hi David,
    Ideally I would like to be able to add anything to that ‘hook’. It might be a button for one site, it might be social media buttons on another etc.
    I know I can style a menu link to look like a button, but that is not really what I am after.
    The way that elements allow you to make anything and show that in a hook is really useful. That is the functionality I am after really.
    So, in short… yes a real button.
    Thank you for any assistance.
    Or am I barking up the wrong tree and is it easier to tweak the off-canvas menu so that it looks like the default menu?
    Thank you

  • So you could do this:

    1. use the generate_after_primary_menu hook.
    This comes immediately after the main menu.

    2. insert a container block ( or div ) with a CSS Class of: after-mobile-menu
    2.1 add your button or whatever inside that.

    3. Add this CSS:

    
    .after-mobile-menu {
        order: 5;
        display: none;
    }
    nav.toggled .after-mobile-menu {
        display: block;
    }
    

    Which will display it only when the mobile menu is open.

  • Thank you David,

    I did as you suggested.

    When the hamburger is toggled (so menu is visible) the button does show if I add some z-index to it to bring it to the front. But it is not appearing in with the menu items, it is sitting on top of them (due to the z-index I know). Without the z-index it doesn’t show at all.

    I am working locally so can’t share my test site with you.

    Can you suggest anything else?

    Thank you.

  • It is extremely difficult for us to help without seeing the issue.

    Is it possible to move the site online and share the site link with us?

  • Hi Ying,

    Thank you for your reply.

    I have created a staging site for you to access. You have the role of administrator. Login details added to the Private information area.

    The button is added as an element.
    I would like it to appear after the CONTACT listing in the mobile menu please.

  • OK, so the issue is two fold, first the Sticky Navigation will absolute position the mobile menu, and to compound that you have the menu set to cover 100vh.

    Instead you will need to hook the button inside the menu

    1. Add this PHP Snippet:

    
    add_filter( 'walker_nav_menu_start_el', 'db_sub_menu_item_hook', 10, 4 );
    function db_sub_menu_item_hook( $item_output, $item, $depth, $args ) {
    
        // Specify menu item class to target
        $class_string = 'gp_menu_hook';
        $menu_item_classes = $item->classes;
    	
        if ( empty( $menu_item_classes ) || !in_array( $class_string , $menu_item_classes ) ) {
            return $item_output;
        }
        $menu_item_slug =  $item->post_name;
        // create custom hook from $class_string and $menu_item_slug
        // example result gp_menu_hook-{menu-item-name}
        $custom_hook_name = $class_string . '-' . $menu_item_slug;
        
        // Add our common and specific hooks
        ob_start(); 
        ?>
        <div class="custom-menu-hook <?php echo $custom_hook_name; ?>">
    	<?php do_action('gp_before_menu_hook'); ?>
    	<?php do_action($custom_hook_name); ?>
    	<?php do_action('gp_after_menu_hook'); ?>
        </div>
        <?php
        $custom_html = ob_get_clean();
        // return the output after the menu items anchor
        $item_output = $custom_html;
    
        return $item_output;
    }
    

    2. in Appearance > Menus add a Custom Link with a relevant label eg. button
    Give the menu item a CSS Class of: gp_menu_hook

    This will create a custom hook inside that menu item. The hook name syntax will be: gp_menu_hook-{menu-item-name}

    So for the example it would be: gp_menu_hook-button

    3. In the Element you can set the Hook to Custom Hook, and paste in your hook name.

  • Perfect. Thank you David. You are a star. Consider this resolved.

  • Glad to be of help

  • Coming from another topic – I also resorted to this solution and it works like charm! Thank you very much, David!!!

  • Glad to be of help 🙂 !!

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