Skip to content



How To Exclude Pages From A WordPress Navigation Bar

WordPress Navigation BarOne of the biggest things a lot of WordPress users look for in a new theme is easy navigation. You can always add Page and Category widgets to your sidebar, but having a well organized navigation bar (or two) can make a huge difference for your blog’s visitors.

Not all themes come with navigation bars built in, and even when they do, you might need to modify them a bit. While a navbar may seem like a simple thing at first, there are actually a TON of different things that you can do to make sure your site is as user-friendly as possible.

The first thing you need to decide is what you want your navigation bar to display. Generally, navigation bars come in two flavors: Categories and Pages. Some blogs (like this one) will use two navbars, one for each option, or even combine the two into one line. The only risk with this last option is that you can quickly find yourself running out of space if you add too many new pages or categories to your site.

How To Switch Your Navigation Bar From Pages To Categories (or vice versa)

If the navigation on your current theme isn’t showing what you want, don’t worry! Switching from Category to Page based navigation is easy!

The first thing we need to do is open up your theme’s header.php file, either by using the theme editor under WordPress’ “Apearance” tab, or by downloading the file with your favorite FTP client.

Your navigation bar is controlled by a template tag, either wp_list_pages or wp_list_categories.

Search your header.php for the tag that applies to you, and if you need to, you can replace it with the one you’d rather be using. The tag will include a number of parameters, grouped in parenthesis, and we’re going to be adding to these in a moment.

Locating the ID of a Page or Category

Now that you’ve found the template tag that’s controlling your navigation, it’s time to add a parameter to hide the pages you don’t want to see. In order to do this, you’ll need each of those pages’ ID numbers.

In addition to the names you give them, WordPress assigns every category and page a unique ID number. To find that number, pull up the page (or category) editing screen of your dashboard. As you hover over each entry, a row of links will appear, one of them being “edit.”

Hover over that “edit” link and look at the bottom bar of your browser. The destination URL of the link is displayed, and ends with the page/category ID. For those of you on a Mac, you’ll just want to click the “Edit” link and look at the URL it takes you to.

At the end of the “Edit” URL, pages will say “post=” and categories will say “cat_id=” followed by a number. That number is the ID you need, and we’ll discuss where to use it a little later on.

How To Exclude Specific Pages or Categories

The most common thing that people want to do is hide a page on on their navigation bar. This could be a “Thank You” page for new subscribers, or an intermediate page for visitors going through a certain process. An example of this would be on this site, when a new client signs up for a WordPress installation, there are a few different pages that guide them through the process of scheduling an appointment, acquiring hosting, etc. These pages are all necessary, but aren’t something I want all over the top of every page.

Hiding a page from your navigation bar is easily accomplished by using one of the several parameters we’ll be going over. This is the (you guessed it!) “exclude” parameter.

To exclude the page you want, enter the parameter, followed by the ID of the page/category in question:

wp_list_pages('exclude=4,37,22')

In this example, I’ve used list_pages, but it works the same way with the categories tag. As you can see I’ve included multiple ID’s, separated by commas. You can just as easily use only a single ID with no comma if you only have one item to hide.

When entering your parameters, don’t forget to wrap them in parenthesis, and enclose them in quotation marks as I’ve done above.

Limiting Navigation To Specific Pages

If you have a situation where you only want to display certain pages or categories on you navigation bar, you can certainly use the exclude parameter to hide all of your other pages, but depending on the size of your site, gathering all of those IDs can be a bit tedious. In addition, every time you create a new page or category, you’ll need to go back into header.php to add the new ID.

Not fun.

The solution is to switch to the include parameter:

wp_list_pages('include=4,37,22')

Once again, this parameter works for both the page and category tags. Using include will display all the pages listed, so if you add new pages in the future, they won’t show up unless want them to (in which case, you’ll add the new ID to the template tag in header.php).

Note: The order that you list the ID’s in does not impact the order the pages will display in. To rearrange the page order, use the “Order” field, under “Attributes” in the right hand column of the the “Edit Page” screen.

Dealing With Multiple Parameters

As I mentioned above, the template tag you are modifying probably already has some parameters already defined. The most common parameter on a navigation bar is probably ‘title_li=.’ This parameter is used to define the title that appears at the beginning of the list.

Generally, a navigation bar doesn’t need a title, so the parameter is left blank, with nothing after the “=” sign. This is the equivalent of saying “don’t display any title.” Here’s an example of that parameter in action:

wp_list_pages('title_li=')

So, how do you combine an existing parameter(s) with your new include/exclude? Like this!

wp_list_pages('title_li=&include=4,37,22')

Nice and easy. :) Just place an ampersand (&) at the end of the existing parameter(s), then add your new one. Note that the entire group of parameters (called a ‘string’) is included within the quotation marks. Make sure those are in place, and that all of your parameters are inside them, and you’ll be good to go!

And that’s it! That’s really all there is to selecting what pages you want to include or exclude from your navigation bar.

Have fun, and if you have any questions, drop them in the comments!

There's a whole lot more you can do with your header and navigation... And with your entire blog, for that matter!

Sign in below, and I'll send you updates on future articles to help you get the most out of your blog!


6 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. Nikolasa says

    HOla – hey I’ve tried the exclude command but it’s not working, is there something else that is overriding it?

  2. Chad says

    @Nikolasa

    It’s possible. Does your theme have any built in options? That’s usually the culprit – make sure it doesn’t have a built in method for excluding pages/categories that’s getting in the way! :)

  3. katrisha says

    Is there a way to display both pages and categories?

  4. Chad says

    @katrisha

    There certainly is! All you have to do is use both tags, one after the other. For example, to show pages first and categories second, you’d use the following:

    <?php wp_list_pages(); ?>
    <?php wp_list_categories(); ?>
    

    Of course you don’t want to forget to include the necessary parameters to make your list display exactly what you want.

    Newer versions of WP make it easier to combine the two list types, with the addition of custom menus that you can build directly in your dashboard – but this function on works if your theme supports it.

  5. Lozz says

    Tested on WP 3.01 works great, thanks
    wp_list_pages('sort_column=menu_order&depth=3&title_li=&exclude='.get_option('woo_nav_exclude'));
    Just added ID after exclude=

  6. Chad says

    @Lozz

    Awesome! Glad this was useful!

    In case it helps you further, based on the code you shared it looks like your theme has an option to exclude pages without having to mess with the code (the .get_option('woo_nav_exclude') part is used to retrieve a user-defined option built into a plugin/theme).

    What you have now should work great – although a benefit of the built in options screen is that if your theme is ever updated, you shouldn’t have to go in and re-do the exclusions :)



Some HTML is OK

or, reply to this post via trackback.