How to exclude pages from wp_nav_menu

The wp_nav_menu is a pretty awesome feature in WP.  Not only It let you to create your desired custom menu, but also other customization. However, the exclude page feature is missing in the default wp_nav_menu accepted parameters . This mean we have to do a little trick to make it do so.

From the WP documentation, we can see that by default the “fallback_cb” is pointing at  ‘wp_page_menu’ . This is the element that we need to do our trick.

So instead let fallback_cb poiting at default ‘wp_page_menu’, we create a custom function for it. In this function, we will include the ‘wp_page_menu’ and as we can see from the documentation of wp_page_menu, it does accept exclude parameter. Hence this is where we define which pages we want to exclude.

In your function.php add this lines:

That’s all!

1 Comment

  • Jascha says:

    Nice idea, but it wont work if you have a created a custom menu. The fallback just shows when there is no menu, so its not suitable to exclude a page from a custom menu :( thanks anyway

Leave a Reply

Your email address will not be published. Required fields are marked *