Site logo

[Resolved] Primary navigation shortcode

Home Forums Support [Resolved] Primary navigation shortcode

Home Forums Support Primary navigation shortcode

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #1941232
    David

    Hello

    I’ve added the primary menu via a shortcode to a block at the top of the page…
    https://staging.michellewilliamson.net/

    The shortcode code I added to functions was…

    //CALL MENU VIA SHORTCODE
    function print_menu_shortcode($atts, $content = null) {
        extract(shortcode_atts(array( 'name' => null, ), $atts));
        return wp_nav_menu( array( 'menu' => $name, 'echo' => false ) );
    }
    add_shortcode('menu', 'print_menu_shortcode');
    

    How can I get the menu to use the header styling, so it breaks to a burger menu on mobile?

    Thanks
    Dave

    #1941426
    David
    Staff
    Customer Support

    Hi there,

    thats quite tricky to do as the GP Navigation is more then just the wp_nav_menu request and it has to be hooked into the theme. Which isn’t straight forward to do.

    You could try the following.

    1. Create a Shortcode that simply outputs a Hook. See here:

    https://generatepress.com/forums/topic/block-element-inside-another-block-element-content/#post-1904275

    in that example it creates a custom hook named: custom_shortcode_hook

    2. Then set the Primary Navigation to display as per a default install and set its location to After Header

    3. Then you can add this PHP Snippet to unhook the navigation and hook it into your short code hook:

    add_action( 'after_setup_theme','db_shortcode_navigation' );
    function db_shortcode_navigation() {
        remove_action( 'generate_after_header', 'generate_add_navigation_after_header', 5 );
        add_action( 'custom_shortcode_hook', 'generate_add_navigation_after_header', 10 );
    }

    This ‘should’ work and keep the themes styling and layout….

    #1941829
    David

    No luck with that David, can you see where I’ve gone wrong?

    #1942738
    David
    Staff
    Customer Support

    Must have a had a brain freeze, the action hook of #3 was completely whack.
    i updated the above code and on the site child theme functions. You can see the nav now… will need some CSS next to style it how you require it.

    #1942753
    David

    Excellent, thank you 😊

    #1942797
    David
    Staff
    Customer Support

    Glad to be of help

    #1945855
    David

    Hi David

    Sorry to be a pain, but I duplicated the shortcode into a different container that sits underneath the header, but only showing it on mobile view and then hiding the one that is in the header image container when on mobile view, as it was scaling the picture when going down to the burger menu.

    The menu now sits underneath the header on mobile which is great, but the menu no longer works. Any reason why?

    Thanks
    Dave

    #1946317
    Elvin
    Staff
    Customer Support

    Hi David,

    The issue lies with the container block you’ve placed the menu in.

    The Container block where the menu was inserted had this CSS:

    @media (max-width: 767px)
    .gb-container-4463f9e5 {
        display: none !important;
    }

    display: none !important; means the script can’t toggle the display property of the menu to show it because the property is applied to the parent.

    You’ll have to write a toggle script for the display property of the Container Block so it shows up when you click the menu icon. 😀

    #1946444
    David

    Any help with the toggle script would be very much appreciated.

    #1946533
    Elvin
    Staff
    Customer Support

    You can try adding HTML Anchor toggled-container to the column container block.

    <script>
    
    let toggleBtn = document.querySelector(".menu-toggle");
    let targetElement = document.querySelector("#toggled-container");
    
    toggleBtn.addEventListener('click',function(e){
    e.preventDefault();
    targetElement.classList.toggle('show-content');
    });
    
    </script>

    and then add this CSS:

    #toggled-container{
    display: none;
    }
    
    #toggled-container.show-content{
    display: block !important;
    }
Viewing 10 posts - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.