0

I’m hoping you can help me out. I’m working on adding an extra custom menu to my header. I would like this to be a hamburger menu with slide-in function which is visible (and working) on desktop and mobile. When viewed on a small screen, the two seperate menu’s should join into one. Tony Robbins gives a good example of what I would like to achieve.

I am fairly new to WordPress and am developing this site locally, so unfortunately I cannot provide a link. What I have done so far:

  1. Registered a new menu in a site-specific plugin:

 function wpb_custom_new_menu() {
  register_nav_menu('slide-in-menu',__( 'Slide In Menu' ));
}
add_action( 'init', 'wpb_custom_new_menu' );
  1. Using the WordPress dashboard, appearance, menu’s: I added pages to this menu

  2. I added this menu to the header of my theme:


<?php wpb_custom_new_menu( array( 'theme_location' => 'slide in menu', 'menu_class' => 'slide-in-menu-class' ) ); ?>
  1. I created a js file for the code, named this ‘slidepanel.js and uploaded it to a newly-made js folder in my childtheme:

(function($) {
$('#toggle').toggle( 
    function() {
        $('#popout').animate({ left: 0 }, 'slow', function() {
            $('#toggle').html('<img src="https://localhost:8888/testwebsite/wp-content/themes/testwebsite-child/images/menu.png" alt="close" />');
        });
    }, 
    function() {
        $('#popout').animate({ left: -250 }, 'slow', function() {
            $('#toggle').html('<img src="https://localhost:8888/testwebsite/wp-content/themes/testwebsite-child/images/menu.png" alt="close" />');
        });
    }
);
})(jQuery);
  1. Then I returned to the header file and wrapped my code so that it would toggle:

<div id="toggle">
    <img src="https://localhost:8888/testwebsite/wp-content/themes/testwebsite-child/images/menu.png" alt="Show" /></div>
    <div id="popout">
    <?php wpb_custom_new_menu( array( 'theme_location' => 'slide in menu', 'menu_class' => 'slide-in-menu-class' ) ); ?>
</div>

Unfortunately though, it doesn’t work, even after numerous attempts. All I see is the hamburger icon, exactly where it should be, but: – it doesn’t toggle open; – it doesn’t combine with my main menu on a small screen, but stays separate.

I’m hoping someone here can help me out. Maybe there’s something wrong with my code? Maybe I’m working in the wrong files? Any help or other suggestions on how to achieve success in this area would be much appreciated!

Thank you

0

I see 2 things.

1 (your point 5).

$('#toggle').toggle( // don't you need a click action here?

And i think you're using toggle() the wrong way.

Example:

$( "#toggle" ).click(function() {
  $( "#toggle" ).toggle( "slow", function() { // The matched elements will be revealed or hidden
    // Animation complete.
  });
});

2.

<div id="toggle">
  <img src="img_url" alt="Show" /></div>
  <div id="popout">
    <?php wpb_custom_new_menu( array( 'theme_location' => 'slide in menu', 'menu_class' => 'slide-in-menu-class' ) ); ?>
  </div> <!-- missing closing div -->
</div>

Greetz, Bjorn

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.