Subscribe to KS RSS RSS Reader Reader RSS Reader Live

Show Only Children Pages on Parent Page

 Posted by at 14:13  Add comments
 
Bookmark and Share

Show Only Children Pages on Parent Page

I’ve been searching the web for hours today trying to find a method to keep my menu structure from scrolling all the way down the page. I’ve looked at various WordPress functions to do the job such as get_page_children(), wp_list_pages() and wp_page_menu() but there doesn’t seem to be a simple function that will list just the current menu hierarchy. But then, just in time, I found the function remove_others_children() written by Jonathan Dingman (http://www.firesidemedia.net/) with assistance from Matt Martz (http://sivel.net/).

The code is as follows, and yes I did copy this code from Jonathan’s website (with his permission) and yes he is in the process of modifying the code so if you want the updates just pop over to his website at http://www.firesidemedia.net/.

To use the code, just add the code to your theme’s functions.php file. This will automatically remove all but the current children pages from your menu structure. The page menu on this website is an example of the codes usage.

 

 add_filter('wp_list_pages_excludes',  'remove_others_children');
 function remove_others_children($excludes =  array()) {
  $all_pages = get_pages();
   $all_children = (array) get_page_children(true, $all_pages);
   foreach ( $all_children as $child )
    $excludes[] = $child->ID;
   if(!is_page())
    return $excludes;

  global $post;

  $this_heirarchy = (array) $post->ancestors;
  $this_children = (array) get_page_children($post->ID, $all_pages);
  foreach($this_heirarchy as $ancestor)
    $this_children = array_merge($this_children, (array)  get_page_children($ancestor, $all_pages));

  foreach($this_children as $child)
    $this_heirarchy[] = $child->ID;

  return array_diff($excludes, $this_heirarchy);
}

 

Related Articles

  3 Responses to “Show Only Children Pages on Parent Page”

  1. i’m new… expectancy to brief nearly more often!

  2. Hi!
    If i add this code to functions.php what code do i need to make it work in theme files? For example in sidebar? wp_list_pages() function still returns all the child pages if the depth is 2.

    • Hi Adam

      I’m sorry to say I’m not that hot on the coding, I just picked up the code from Jonathan, droped it into my site and it worked. If you need to know more about the code I suggest you nip over to Jonathan Dingman’s site.

      If you need more control over your index listings, may I suggest a pluging I found just the other day – Dagon Design Sitemap Generator. My sitemap page is gerated using this pluging, it works really well I think.

      Hope that helps, Philip

 Leave a Reply

(required)

(required)

Copyright © Philip King 2009 King Solutions (UK) - ClickBank - Justhost - Wishlist Member - Wishlist Member About - Contact Us - Privacy Policy - House Rules - Site Terms - Earnings and Income Disclaimer Suffusion theme by Sayontan Sinha
95 queries in 0.830 seconds (Child).