0

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.

1
  • do you mean a wp_nav_menu?
    – Milo
    Oct 10, 2013 at 20:21

1 Answer 1

1

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);
1
  • Exactly what I was looking for, many internets for you, sir!
    – Joren
    Oct 10, 2013 at 21:27

Your Answer

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

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