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.

How do I make a page (dis)appear conditionally in a menu? I had something like this in mind:

if(condition) 
    //show_page_in_menu

I would prefer an answer that doesn't incorporate an external plugin.

share|improve this question
    
do you mean a wp_nav_menu? –  Milo Oct 10 '13 at 20:21
add comment

1 Answer 1

up vote 1 down vote accepted

You can use wp_nav_menu_objects or wp_nav_menu_items hooks to add your filter function.

function my_hide_menu_items($objects) {
    if ( is_admin() ) return $objects;

    foreach ( $objects as $k=>$object ) {
        if ( YOUR CONDITION ) {  // if $object shouldn't be displayed
            unset($objects[$k]);
        }
    }
    return $objects;
}
add_filter('wp_nav_menu_objects', 'my_hide_menu_items', 10, 2);
share|improve this answer
    
Exactly what I was looking for, many internets for you, sir! –  Joren Oct 10 '13 at 21:27
add comment

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.