WordPress Tip: How To Exclude Pages From wp_nav_menu Function

In this tutorial, I will show you how to exclude certain pages from the navigation menu when using the wp_nav_menu() function in WordPress. The wp_nav_menu() function is usually located in the header.php file in your theme´s folder.

To my surprise, I discovered that the wp_nav_menu() does not contain “exclude” as one of its parameters. So in this case, how to prevent certain pages from showing up in the menu?

The wp_nav_menu() purpose is to display the navigation menu created in WordPress dashboard in Appearance › Menus panel if a menu is set there, and with a fall back scenario if it is not.

How to exclude pages that you don´t want to show in the Navigation Menu? This is where this tutorial helps. This tutorial applies to any WordPress theme that uses the wp_nav_menu() function.

Real Example | WordPress Twenty Eleven Theme

To give you an example. One of the popular WordPress themes is Twenty Eleven.

Below is the original code of the header.php file of the Twenty Eleven theme (Version 1.3). Many unrelated sections of the code are removed for brevity.

CODE-x: Original header.php File :: Twenty Eleven Theme (Version 1.3)

Note: To scroll within the code, you can also click on the code window and use your keyboard´s arrow keys.

<?php
/**
 * The Header for our theme.
 * Displays all of the <head> section and everything up till <div id="main">
 * @package WordPress
 * @subpackage Twenty_Eleven
 * @since Twenty Eleven 1.0
 */
?>

<!-- CODE REMOVED FOR BREVITY, by Boutros AbiChedid. -->

<?php /* Our navigation menu.  If one isn&acute;t filled out, wp_nav_menu falls back to wp_page_menu. The menu assiged to the primary position is the one used. If none is assigned, the menu with the lowest ID is used. */ ?>

<?php wp_nav_menu( array( 'theme_location' => 'primary' ) ); ?>
                        
<!-- CODE REMOVED FOR BREVITY, by Boutros AbiChedid. -->

<div id="main">

CODE-x Notes:

For the purpose of this tutorial, the most important statement is line 15 of CODE-x.

The wp_nav_menu() works on WordPress 3.0 and above. I hope you will always upgrade to the latest version.

Also WordPress Twenty Ten theme (Version 1.3) uses the same wp_nav_menu() function but with different arguments. More on that later.

Code Reference: wp_nav_menu()

Result of Twenty Eleven Theme Using CODE-x

The image below, shows how the Menu appear for this Blog when I use the Twenty Eleven theme without any modifications to the wp_nav_menu():

Twenty Eleven Theme for this Blog: Without Excluding any Pages from the wp_nav_menu() function.

But, I don´t Want to Show Certain Pages in the Menu. What Should I do?

To exclude certain pages from the navigation menu, you need to add the “exclude” argument to the wp_nav_menu() function as shown in the code below. This works, even though the exclude parameter is not one of the parameters listed in wp_nav_menu() function.

You have the option to exclude pages from your Menu, by changing line 15 of CODE-x. For instance, I wanted to exclude 3 pages from the menu; thus I replaced line 15 of CODE-x with the following line (Line-xM).

Line-xM

//Replace line 15 of <em>CODE-x</em> with the following line:
<?php wp_nav_menu( array( 'theme_location' => 'primary', 'exclude' => '859,889,1031') ); ?>

Note: The numbers 859, 889 and 1031, are separated by commas, and are 3 page IDs on this blog, you need to replace these numbers with yours. If you don´t know how to find the Page ID, read my previous tutorial on How To Find the Page ID.

Result of Twenty Eleven Theme When Using Line-xM

The image below, shows how the Menu appear for this Blog when I use the Twenty Eleven theme when modifying the wp_nav_menu() (using Line-xM):

Twenty Eleven Theme for this Blog: After Excluding 3 Pages from the wp_nav_menu() function.

If you want to exclude just one page from the Menu, then replace line 15 of CODE-x with the following line:

//Replace line 15 of <em>CODE-x</em> with the following line:
<?php wp_nav_menu( array( 'theme_location' => 'primary', 'exclude' => '859') ); ?>

Note: the number 859 is a page IDs on this blog, you need to replace this number with yours.

I am Using WordPress Twenty Ten Theme. Can you Help me With That?

Sure I can! Open the header.php file located in your theme´s folder and find the wp_nav_menu function. For instance for Version 1.3 of Twenty Ten theme, the statement that you will be replacing is line 12 of the following code:

CODE-y: Original header.php File :: Twenty Ten Theme (Version 1.3)

<?php
/**
 * The Header for our theme.
 * Displays all of the <head> section and everything up till <div id="main">
 * @package WordPress
 * @subpackage Twenty_Ten
 * @since Twenty Ten 1.0
 */
?>

<!-- CODE REMOVED FOR BREVITY, by Boutros AbiChedid. -->

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

<!-- CODE REMOVED FOR BREVITY, by Boutros AbiChedid. -->

<div id="main">

Depending of what you need to do: You have the option to exclude pages from your Menu, by changing line 12 of the CODE-y. For instance, I wanted to exclude 3 pages from the menu, then I replaced line 12 of CODE-y with the following line:

//Replace line 12 of <em>CODE-y</em> with the following line:
<?php wp_nav_menu( array( 'container_class' => 'menu-header', 'theme_location' => 'primary',  'exclude' => '859,889,1031')); ?>

Note: The numbers 859, 889 and 1031, are separated by commas, and are 3 page IDs on this blog, you need to replace these numbers with yours.

If you want to exclude just one page from the Menu, then replace line 12 of CODE-y with the following line

//Replace line 12 of <em>CODE-y</em> with the following line:
<?php wp_nav_menu( array( 'container_class' => 'menu-header', 'theme_location' => 'primary',  'exclude' => '859')); ?>

Note: The number 859 is a page ID on this blog, you need to replace this number with yours. If you don´t know how to find the Page ID, read my previous tutorial on How To Find the Page ID.

Your Turn to Talk

Now you know what to do if you want to exclude certain pages from the wp_nav_menu() output. If your theme is using the wp_nav_menu() function and you don´t want certain pages from showing up in your menu, you know what to do.

This tutorial applies to any WordPress theme that uses the wp_nav_menu() function to display the navigation menu. The exclude parameter can still be used, even though it is not listed as one of the parameters in the WordPress Codex.

If you have something to add or anything else to say, please share your opinion or questions in the comments section. Your opinion matters, unless it is a Spam.

If you found this post useful, please consider: linking back to it, subscribing by email to future posts, or subscribing to the RSS feed to have new articles delivered to your feed reader, or feel free to donate. Thanks!

About the Author |
Boutros is a professional Drupal & WordPress developer, Web developer, Web designer, Software Engineer and Blogger. He strives for pixel perfect design, clean robust code, and user-friendly interface. If you have a project in mind and like his work, feel free to contact him. Connect with Boutros on Twitter, and LinkedIn.
Visit Boutros AbiChedid Website.

5 Responses to “WordPress Tip: How To Exclude Pages From wp_nav_menu Function”

  1. Candy says:

    hi there, in your example, if i have 2 sub menu under the page “Resources”, and i don’t want the “Resources” page to be clickable (click on it and no respond, does not link to the page), can u plz tell me how to do that? i see in some websites that it shows “javascrip:;” when u put the mouse over the words, but i can’t find the place to revise this. thanks!!

  2. rohan says:

    thank you for this i waste my 2 hour for that issue thank you very very much

  3. Ahmed says:

    Too bad, sir. It doesn’t work for me. :(

    I’m using zBench theme.


    'primary', 'fallback_cb' => 'zbench_wp_list_pages', 'container' => 'false', 'items_wrap' => '%3$s' ) ); ?>

  4. Manish Chakma says:

    Today i spend 6 hours for solve this problem and now i exclude a page from my nav menu. Thanks so much.