Hiding Subpages (Children Pages) in WordPress
As I was getting my template refined for the implementation of this site, I came across an interesting little code snippet.
What was I trying to do? I was working on adding what WordPress calls "Pages" to my site to use as static content for my project examples. Once I had added my pages, I needed to display these pages on my header.php. In order to do so, WordPress provides a function. Here's what the code looks like:
<?php wp_list_pages(); ?>
The issue with this function is it provides an entire list of links to each of your pages that you have added to WordPress. In my case, I had set up a number of Pages to be children [pages linked off] of the "Project Examples" page. It turns out that you can provide to the wp_list_pages() function an argument defining the depth of pages shown. If you wanted to only show one level of pages, you would use the value '1'.
I was able to choose the levels deep I wished to display pages from the parents. In doing this, the function would no longer display all my pages but rather just the top level. It's now up to me to manually link to the other existing pages in my content management system.
The final code:
<?php wp_list_pages('depth=1'); ?>
Note, you can also exclude individual pages from being displayed using this function. In order to do so, provide the id's of each page you would like to hide and format as such:
<?php wp_list_pages('exclude=12,43'); ?>
It turns out there is quite a bit you can do with this function. Check out the official WordPress docs for more


July 7th, 2008 at 12:26 am
I just wanted to say Thank You for this! I've been hunting and searching for days for a way to hide childpages in my sidebar.
October 6th, 2008 at 10:02 pm
Oh my gosh, you're a genius. This is the full code I had to use to also get the tabs in the right order. Great insight!
January 16th, 2009 at 4:06 pm
it's so easy but it took me so long to find yr page.
Thanx,
Higgins
March 3rd, 2009 at 6:30 am
Hi, thanks for the code.
I'm having a slight problem with it though.
My main navigation lists all my static pages, even the subpages. I only want it to display the first level pages, so I tried implementing your code.
The code for my page list looks like this:
The "('title_li=')" needs to be there otherwise the styling in my navigation goes awry. Where exactly in that bit of code do I add the "('depth=1')" bit?
If I do for instance my subpages are still displayed
So I guess my question is (since I'm no PHP expert): How do I add two separate arguments ('title_li=' and 'depth=1') to one tag?
Thanks for any help.
October 7th, 2009 at 8:15 am
Is it possible to hide them by collapsing and revealing them by mouse-over?
October 18th, 2009 at 1:34 pm
i know this post is a little late but… Lory, simply separate the arguments with an ampersand '&'