Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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') ); ?>
share|improve this question
1  
Where exactly are you loading that script? The error comes from a PHP file while you showed us Javascript code... –  kaiser Aug 26 '13 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 –  Andrea Puiatti Aug 26 '13 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 Aug 26 '13 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 –  Andrea Puiatti Aug 26 '13 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 Aug 26 '13 at 16:19
add comment

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.