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 functions. Check out the official WordPress docs for more

Related Posts

  • WordPress 2.2 Released!
  • Remove Fancy Quotes in WordPress
  • Ryboe Tag Cloud 1.02 Released
  • Ryboe Tag Cloud Plugin for WordPress
  • Ryboe Tag Cloud V.1.01 Released
  • One Response to “Hiding Subpages (Children Pages) in WordPress”

    1. Jen Says:

      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.

    Leave a Reply