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 want to plain to cache a page menu, that takes about 50 queries on page load.

At the moment i have cahche setting code with Transients_API

<?php
                        $housemenu = get_transient('housemenu_key');

                                if (!$housemenu) {

                                    $housemenu = wp_nav_menu( array('menu' => '','menu_class' => 'sf-menu','menu_id' => 'nav','walker' => new description_walker()));
                                    echo 'not-set';

                                    set_transient('housemenu_key', $housemenu, 60*60*3);
                                }

                        echo $housemenu;
                        ?>

$housemenu = get_transient('housemenu_key'); returns a nothing. If i set like this:

$housemenu = 'return corrent'l
echo 'not-set';

All works perfect. Why this menu is not working? Menu can be a walker transient?

Use: Wordpress 3.8

share|improve this question

2 Answers 2

wp_nav_menu() doesn't return anything by default, it just echoes out formatted html.

For wp_nav_menu() to return a value you will need to pass 'echo' => false into it's arguments, like so:

$housemenu = wp_nav_menu( array(
    'menu' => '',
    'menu_class' => 'sf-menu',
    'menu_id' => 'nav',
    'walker' => new description_walker(),
    'echo' => 'false'
));

It should be noted that by caching the entire menu in a transient, you will lose it dynamic nature. For instance if it cached it while on your home page all of the active classes applied to the menu items will ALWAYS reflect the home page. Thus if you were to visit a different page the active classes on the menu items would be wrong.

This will cause quite a bit of pain if you wish to convey via CSS which page the current user is on.

share|improve this answer
    
The same, dont' work with set_transient and get_transient –  Foxsk8 Jan 14 at 22:12
    
It does indeed work as I've just tested it out on my local. Is what you have above an exact copy of the code you are using? Does your wp_nav_menu() function generate a menu when 'echo' => true with the current settings? –  MichaelJames Jan 14 at 22:24
2  
Do a one-time delete_transient('housemenu_key'); Then your menu will start working. The reason it is not working for you now is because you still have the old empty transient set from your previous attempts. Or just wait three hours for the transient to expire. –  Otto Jan 14 at 22:38
    
"It should be noted that by caching the entire menu in a transient, you will lose it dynamic nature. " -- and not just stuff like CSS classes (current_item), but also the links themselves. Loading the site over HTTPS makes links start with HTTPS instead of HTTP; using a translation plugin like WPML can change the menu entirely, to translated pages; using a mobile domain with a mobile switcher plugin can change www. to m. or other mobile domain variant; etc. –  webaware Jan 15 at 2:26

By default wp_nav_menu() echoes result and returns nothing. You need to set 'echo' => false in its arguments to flip the behavior and be able to assign result to a variable.

share|improve this answer
    
Not working. Menu shows up, but $housemenu = get_transient('housemenu_key'); is like a empty and then gos to function and each time menu is set over and over $housemenu = wp_nav_menu( array('menu' => '','menu_class' => 'sf-menu','menu_id' => 'nav','walker' => new description_walker(),'echo' => false)); –  Foxsk8 Jan 14 at 21:56

Your Answer

 
discard

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

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