0

I'm trying to dynamically load the wp_menu with enquire.js but it just doesn't work. If the screen is < 1080px the wp_menu(mobile) should be loaded, otherwise the wp_menu(desktop).

In my homepage-template.php I regularly call the header and then I use enquire.js as per instructions.

<script type="text/javascript">
enquire.register("screen and (max-width:1080px)", {

// OPTIONAL
// If supplied, triggered when a media query matches.
match : function() {
    $("body").load(
      "http://dev.andreapuiatti.com/dev4/wp-content/themes/spk/templates/headernav-phone.php"
    );
    document.write('meno di 1080px<br/>');
},

// OPTIONAL
// If supplied, triggered when the media query transitions
// *from a matched state to an unmatched state*.
unmatch : function() {
    $("body").load(
      "http://dev.andreapuiatti.com/dev4/wp-content/themes/spk/templates/headernav-computer.php"
    );
    document.write('OLTRE i 1080px<br/>');
},

// OPTIONAL
// If supplied, triggered once, when the handler is registered.
setup : function() {},

// OPTIONAL, defaults to false
// If set to true, defers execution of the setup function
// until the first time the media query is matched
deferSetup : true,

// OPTIONAL
// If supplied, triggered when handler is unregistered.
// Place cleanup code here
destroy : function() {}

});
</script>

Enquire works, but Wordpress doesn't and kicks out this error message:

Fatal error: Call to undefined function wp_nav_menu() in /kunden/372333_10437/webseiten/dev.andreapuiatti.com/dev4/wp-content/themes/spk/templates/headernav-computer.php on line 20

Any Idea on how to dynamically load that menu?

follows the content of headernav-computer.php:

<?php wp_nav_menu( array('link_after' => '<div class="one"></div>', 'menu' => 'primary', 'container_class' => 'menu') ); ?>
5
  • 1
    Where exactly are you loading that script? The error comes from a PHP file while you showed us Javascript code...
    – kaiser
    Commented Aug 26, 2013 at 15:39
  • the js code has been placed directly into the homepage-template.php. Enquire.js has been regularly enqueued through wp_enqueue_script in functions.php Commented Aug 26, 2013 at 15:45
  • 2
    You can't load a template file directly, there is no WordPress environment in that context. have a look at the AJAX API.
    – Milo
    Commented Aug 26, 2013 at 15:49
  • Not sure if it makes any difference but 1) enquire.register registers a breakout resolution and execute a function when this breakout is reached; 2) the function call ajax that load html and put in the body Commented Aug 26, 2013 at 15:55
  • 1
    Like @milo said, wp_nav_menu() is undefined because it is being called outside the Wordpress environment. If you build this as a plugin you would have access to the Wordpress environment.
    – epilektric
    Commented Aug 26, 2013 at 16:19

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.