WordPress wp_nav_menu to Exclude Page(s)

While working on a project I realised that I need to exclude pages I created from the top menu, which is built by wp_nav_menu function in header.php. I could use Menus under Appearance tab to define a top menu and pass the menu name in the function arguments but I would loose the CSS for the original menu created by wp_nav_menu itself. So I started looking for ways to exclude the pages I don’t want to show on the top menu. So I kind of added a hack in wp_nav_menu function.

First, I added “exclude” in the list of default arguments:

$defaults = array( 'menu' => '', 'container' => 'div', 'container_class' => '', 'container_id' => '', 'menu_class' => 'menu', 'menu_id' => '', 'echo' => true, 'fallback_cb' => 'wp_page_menu', 'before' => '', 'after' => '', 'link_before' => '', 'link_after' => '', 'depth' => 0, 'walker' => '', 'theme_location' => '', 'exclude' => '' );

Then I copied couple of lines from wp_list_pages function (in post-template.php) to add the $exclude_array variable:

$exclude_array = ( $args->exclude ) ? explode(',', $args->exclude) : array();
$args->exclude = implode( ',', apply_filters('wp_nav_menu_excludes', $exclude_array) );

Note that I had the change $args[‘exclude’] to $args->exclude. Now in header.php just add the exclude in the argument array:

< ?php wp_nav_menu( array( 'container_class' => 'menu-header', 'theme_location' => 'primary', 'exclude' => '66' ) ); ?>

Done! The unwanted pages are gone from the top menu. Booyaaah!

Profession: Web Application Developer Employment: Technical Lead, Interactive Division, GANZ Location: Toronto, Canada

You Might Also Like

  • Great bit of code, worked a treat first time on my new site. Thanks 🙂

    • Glad it helped. 🙂

  • Jacob, thanks for your comment. I believe 3.3.1 or from an earlier version WP added that. When I worked on it and created this “hack” I went through their documentation and couldn’t find the exclude option, therefore, I had to do this. Moreover, I know the way I’ve done it was wrong because of the reason you mentioned, upgrade will break it. But once I started working on WP more I realised there are other ways you can do it, add action, add hook etc are the ways to deal with your customized function behaviour.

    As developer/programmer we do/create lot of works/hacks which becomes obsolete in future. Not sure if it was my hack which inspired WP to add the “exclude” option or not but it is good to see the option is there by default now.

  • jacob

    Many thanks for your post it was a great help. One thing to mention
    is that you have executed a lot of additional work.

    In the wp_nav_menu() function it checks for any applied menus, if you
    did not create a menu it will use a user_callback.

    The default is ‘wp_page_menu’ the callback also uses the $args variable to
    pass to the wp_page_menu() function of which it accepts the exclude variable.

    So to cut a long story short there is no need to hack anything (hacking is a pretty
    bad idea as after an upgrade is completed things tend to stop working) just
    add an exclude option to your wp_nav_menu() statement and that’s it done.

    (note the information expressed above is based on the use of WordPress 3.31)

  • andrew

    wp_nav_menu( array(‘container’ => ‘div’, ‘container_class’ => ‘menu-wrapper’, ‘container_id’ => ‘main-superfish-wrapper’, ‘menu_class’=> ‘sf-menu’, ‘theme_location’ => ‘secondary’, ‘exclude’ => ‘831,1216,100’) ); is not working

    but how do i create theme location ?
    how do define theme_location ?
    as i was create a new menu call logout-menu is that can be theme_location

  • Thanks for the step by step guide. One question though How can I erase extra links that are showing on top navigation (every time I create a new page through dashboard) from my wordpress twenty 10 theme

  • Torte

    Hi Ehsan!
    First, thank you for your helpfull Post.
    I implement the code from your website and it works fine, but only in the default-menu.
    If i choose a menu with a theme_location parameter (like: ‘theme_location’ => ‘secondary’),
    it don’t exclude the registered sites in the ‘exclude’ parameter.

    Another question is the ‘wp_nav_menu_excludes’ hook. For what or where do i need it?

    wordpressversion: 3.2.1 , modified theme: Twenty Eleven 1.2
    thanks! torte

  • I’m building a website for condo units, when I had to create a page which I didn’t want to be included in my nav menu. Thanks for this post, I found that I need to use the argument 'exclude' => $id in order to do just that. 🙂

  • Pingback: Yosh()

  • Thanks for sharing. Nice article. I’ll tweet it !

  • gràcies!!!!!!!!!!!!!!!!!


  • Gurpreet Singh

    This is really very good. just copy and paste its working fine..

  • Very helpful. Keep going. Thanks.

  • How can I use this attribute to hide pages if the user is
    not logged in? I asked this question on the wordpress support, but
    no reply yet: So
    any suggestions? This is my code here: 'main',
    'menu_id' => 'menu')); ?>

  • Thanks a lot, I was looking for this !