WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

i have a problem displaying only the menu with wp_nav_menu, in which the current-menu-item is in.

Heres what i got:

function register_menus() {
      'sidebar-menu' => __('Sidebar Menu'),
      'first-menu' => __('Menu 1'),
      'second-menu' => __('Menu 2'),
      'third-menu' => __('Menu 3')
add_action('init', 'register_menus')

and on the page.php i'm doing

<?php wp_nav_menu(array('theme_location' => 'first-menu')); ?>
<?php wp_nav_menu(array('theme_location' => 'second-menu')); ?>
<?php wp_nav_menu(array('theme_location' => 'third-menu')); ?>

when i now open up a page, which is in Menu 2, i can style it with .current-menu-item - so no problem here.

but i want to display only the one menu, which has the .current-menu-item in it (Menu 2). the other ones (Menu 1, Menu 3) should be display: none;

is there a way to accomplish that? i thought of a walker, a filter or a simple if-statement, which checks for the menu, which has the .current-menu-item but i can't handle it myself :-(

thank you very much

share|improve this question
ok, i think theres no css-way until css4. so i used a JS, simulating this css4 behavior github.com/Idered/cssParentSelector – hardysim Dec 3 '12 at 10:32
Possible duplicate: wordpress.stackexchange.com/questions/75607/… – mrwweb Jul 17 '14 at 18:19

Just intercept the nav classes and search for the current-menu-item class. This will give you the $item it's associated with.

add_filter( 'nav_menu_css_class', 'get_active_class', 10, 2 );
function add_active_class( $classes, $item )
    if ( in_array( 'current-menu-item', $classes) ) {
        // We found the active class!: $item

    return $classes;

Now just grab your nav menu and apply the needed css classes or style="display:none" to the others.

share|improve this answer

Your Answer


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.