Show Only Children Pages on Parent Page

Tech

There is still much to be desired within WordPress when it comes to dealing with parent and children pages. The core code is there, and it’s usable, but there’s not a lot “out of the box” to play with.

We’ve developed a bit of code to help make your life a little easier.

Adding the code below to your functions.php file will let you remove the other children pages while on the parent page.

[sourcecode lang=”php”]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);
}[/sourcecode]

Here are some examples of what it would like without the code, then with the code.

WordPress:  Remove Children Pages from Parent Pages

A huge thanks goes out to Matt Martz for working with us on helping get this code ready.

16 thoughts on “Show Only Children Pages on Parent Page

  1. Wow, can’t wait to test this out, I’ve been looking for something similar for a couple of weeks, great timing πŸ™‚

    Thanks!

    1. Sure thing! Glad I could help. I’m hoping to come up with some variations of it too, in the near future, to have more flexibility with pages and children pages. That’s one thing I feel is still lacking, is flexibility.

  2. Hi Jonathan, really thanks for sharing this piece of code. Very useful. If there was a need, I was always using different is_page() || $post->post_parent conditions to achieve this behaving, but your solution is just perfect. I’ve just tested it and seems to be working all sexy.

  3. Hi Jonathan.

    I’ve been searching for this trick for hours. You just saved me from having to research the function and write it myself. I’ve implemented the code on my blog and will be giving you a write-up within the next couple of hours. I hope you don’t mind that I’ve reprinted your code on my blog, if this is a problem just let me know and I will remove it.

    Once again, many thanks for a great piece of code.

    1. Sure thing! Glad we could help. All that I ask is that you link back here, that’s all πŸ™‚

  4. THANK YOU! This is so much easier than using a plug-in, and yes I’ll put a trackback here when the site is done.

  5. I’ve tried to use this, but i get a list of all pages and subpages on every page. Sorry, i’m not very savvy with PHP and wanted to know what code i need to put in my sidebar to make this work in the first place.

  6. I would need to do this thing too, but i just don’t get it to work. could it be any problem with WP3.0 .
    I would be so glad for any hint …

  7. I Agree. This rocks!

    Is there an easy way to add more depth? If for instance “Fireside Child 5” has a “Fireside Child 5 Grandchild 1” and “Fireside Child 6” has a “Fireside Child 6 Grandchild 1”, and page “Fireside Child 6 Grandchild 1” is active, how can I hide “Fireside Child 5 Grandchild 1” from the menu?

    Anyway. THANX!

  8. Do you have an example of how to use this code? I’m currently doing this:

    if($post->post_parent){
    $children = wp_list_pages(“depth=1&title_li=&child_of=”.$post->post_parent.”&echo=0″);
    }
    else {
    $children = wp_list_pages(“depth=1&title_li=&child_of=”.$post->ID.”&echo=0″);
    }

    And I want to exclude pages from the parent. Thanks.

  9. Is there a way to modify the code to exclude grandchildren and only show them when you are on a child page?

  10. Thanks so much for this! I spent ages playing with plugins which didn’t do what I needed. Your code was perfect for my project.

  11. Thanks for your tutorial, πŸ™‚
    I have problem, friend. I want to show the current parent title and children title in sidebar continuously, based on page active.
    Example :
    My Page :
    Info
    >Contact

    If i selected Contact, i want to show parent title β€˜Info’ too. So, the display like this in the sidebar:

    INFO
    – Contact

    Here is my code :
    post_parent);
    $children = wp_list_pages(‘title_li=&child_of=’.$post->ID.’&echo=0′);

    if ($children) { ?>

    Help me.. please.
    Thanks before

Comments are closed.