Creating ‘hidden’ pages in wordpress which don’t appear in the navigation menu

Yeah I know theres seventy thousand or so articles out there already talking about modifying

wp_list_pages();

to include and exclude various pages from your wordpress site menus, but I’m going to talk about they way I did it anyway =)

The deal is, I’m wanting to publish stories and other standalone type pieces of writing on this site, and I want to create them as nice friendly text wordpress pages, but I don’t want each and every story to appear as a default pages link. The titles are long, there’ll be a few of them, and they’ll screw up my page layout if included in the pages menu(s), especially in the top nav bar where theres limited space.

To change this behavior, you just need to go into your theme template files and add some parameters to wp_list_pages(). The definitive list of parameters is over here and is worth a read:

http://codex.wordpress.org/Template_Tags/wp_list_pages

The most useful ones for this task are ‘include’, ‘exclude’, and ‘depth’. You can string multiple parameters together using the ampersand & character like so:

wp_list_pages(foobar=1&moobar=2);

Include and exclude will do as they suggest, given a comma delimited list of page numbers. If you use include it will only include the specified pages, if you use exclude it will include all except the specified pages.

I decided a better way to get it done, actually ideal for my purposes, is to create all my ‘hidden’ pages in a subcategory, and set the depth parameter to ‘1’. This means any page created as a subcategory will not appear in the navigation menus, though it can be explicitly linked to, or appear in a list of links for that subcategory (exactly what I want).

The Widgets file

I managed to sort out the top nav bar page listing on my template pretty easily by making the change to the header.php file, but I couldn’t find anything immediately obvious generating the page listing in sidebar.php, or any of the other theme includes. After some bumbling around trying to figure this out, I discovered the sidebar listing was being handled by wordpress widgets . This was pretty easy to fix once I found out where the widgets file is (/wp-includes/widgets.php), although since it uses an array for the parameters its slightly different than the edit above.

You have a few options for changing how the pages display through the wordpress widgets control panel, but sadly no way to exclude sub-pages by default. This would be a cool thing to have available, and I’d write it into the widget file and make my version available, but my php is far too rusty at the moment for this even though its pretty simple.

Ok, how to do it manually. Open up the widgets file for editing and find the section which defines function wp_widget_pages and add to the line which starts as follows:

$out = wp_list_pages (array ('title_li' => '',

etc etc and add to it the parameter ‘depth’ => 1, so it ends up looking something like this:

$out = wp_list_pages( array('title_li' => '', 'depth' => 1,'echo' => 0, 'sort_column' => $sortby, 'exclude' => $exclude) );

Thats it. save the file and your side menu should hide all pages in sub-categories.

Also, there is this plugin I found over here which interferes somehow with

wp_list_pages();

to cause the change everywhere the function is called. Might save having to mess with multiple places in the template / widgets file depending how your theme is set up. (Mine has pages listed in a top nav bar as well as a side menu, for example).

Leave a Reply to Debbie Cancel reply

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