Nullvariable

Easily Exclude Pages from Menus – WordPress How To

by Doug on September 1, 2009

Don’t want your legal disclaimer page to show up in your WordPress page menu? What about the Privacy Policy? Here’s a simple function you can add to your functions.php (should work with almost any theme) that will let you set a custom tag on a post to exclude it from any WordPress pages list.

Here’s the code that makes it happen:
(replace “themename” with the name of your theme or something unique)

add_filter('wp_list_pages_excludes','themename_exclude');
function themename_exclude($output = '') {
/* returns posts with a custom exclude tag so they don't display in the menus */
$pages = get_all_page_ids();
foreach ($pages as $pageid) {
$is_excluded = get_post_custom_values("exclude",$pageid);
if ($is_excluded[0] == 'yes') {
array_push($output, $pageid);
}
}
return $output;
}

exclude-page-example
Now you need to add a custom tag to any post that you don’t want to display in any menu lists. Edit or create the page you want to exclude from the menu, scroll down to the “Custom Fields” box (location will vary depending on what plugins you have installed). The first time you use this custom value you’ll need to click “Enter new” then type in “exclude” with a value of “yes” (remove the ” marks). After you’ve used the value once you will just have to click the select box and choose “exclude” and enter a value of “yes”, if you want to allow the page to show up again, simply remove this value.

This code has not be extensively tested and may require modification to work. This code has not been tested with Thesis, WP-Cache or really anything. I just know that it works with the themes that I’ve developed for WordPress 2.6 through 2.8. Use at your own risk.

Nerd Alert!

Keep reading if you’d like to see how it works.

The first line of code:

add_filter('wp_list_pages_excludes','themename_exclude');

tells WP to add our function “themename_exclude” to the filters that it executes when the wp_list_pages() function is called. Filters allow plugins and themes to modify the default output from WP. In this case we’re asking for the filter that runs against the exclude section of wp_list_pages() so that we can add our universal exclusions whenever the function is called.

Next, we define our function:
function themename_exclude($output = '') {
the function grabs the value sent to it by the filter or defaults to a null value.

There might be a more efficient way to do this but for now we grab all the pages that WP lists in the database with this line of code:
$pages = get_all_page_ids();

Now we will loop through these pages:

foreach ($pages as $pageid) {

With each page we’ll ask WP if that page has a custom value of “exclude” attached to it. This way we don’t have to pull the whole post from the data base, just this field, if it exists:

$is_excluded = get_post_custom_values("exclude",$pageid);

If the value “exclude” exists and the value is “yes” we add the page id to the array that was provided to us at the start:

if ($is_excluded[0] == 'yes') {
array_push($output, $pageid);

Now we have an array ($output) containing the original values that we’re excluded plus any pages that have the “exclude” custom tag. This lets the customer or us modify what pages are displayed or not on a page by page basis without manually editing every wp_list_pages() tag that shows up inside a theme.

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • email
  • Ping.fm
  • RSS
  • StumbleUpon
  • Twitter
  • Yahoo! Buzz
  • amccall
    this rocks! thanks so much :) :) :) and thank you for explaining it, that helps me learn!! yay!
  • Great! Glad I could help!
  • Donna
    This is very useful. I was trying to figure out a way to exclude member-only menu options if the user is not logged in and it seems that a minor modification to this code will do the trick. I'm going to try it but it seems like it should work! Thanks for posting this.
  • yep! you could wrap it in a if ( is_user_logged_in() ) statement.
  • joelfisher
    Nice approach, but I personally like to use the Exclude Pages plugin. It's nice and easy for end users.

    http://wordpress.org/extend/plugins/exclude-pages/
  • OK so I checked out this plugin but it takes 300 lines of code to do what I did in 30...granted a lot of that code goes to making it compatible with older version of WordPress (not sure why anyone in their right mind would be running an old version but that's neither here nor there). But it just seems like a lot of the code is useless. I'm reading up and seeing how hard it would be to add a checkbox to control the option.
  • I figured there was a plugin to do this but plugins have a lot more overhead than I wanted for this project...
  • For the code-challenged, there's also an "Exclude Pages" plugin that gives you a checkbox in the right-hand sidebar in the new post page that lets you toggle whether it shows in the navigation or not.

Previous post:

Next post: