Support » How-To and Troubleshooting » wp_list_pages – only want children to top level

wp_list_pages – only want children to top level

  • Hello!

    I got a problem, and i dont really know how to take care of it. Appreciate any insight into this delicious problem (or at least, it is for me).

    I have a main menu with 5 subpages at the top. They have some children, which have a few children.

    Under my mainmenu i have a subpages menu thats supposed to display subpages whose parent is toplevel.

    I dont know how to do that. I only want pages whose parent is the top to be displayed. As it is know even third level pages are display if i go that deep.

    If this doenst make any sense to you at all have a look at http://www.ufstockholm.se

    What im using now is

    	$children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0&depth=1");
    $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0&depth=1");

    Im yet to master the post_parent, please help me! 🙂

Viewing 3 replies - 1 through 3 (of 3 total)
  • BUMP

    I am now looking into this too.
    I want to have the top-most and second level menu items to show … once I am on the third level, the list subpages even if on subpage code will replace the second level with the current page’s siblings, which is the third level.

    Damn, this is so hard to explain.

    Okay, I got it. It’s not a solution for everyone, but for me, with a setup of grandparent, parent and child pages (a depth of 3), this works fine:

    if($post->post_parent) {
    	$parent = get_post($post->post_parent);
    	if ($parent->post_parent) {
    		$children = wp_list_pages("depth=1&title_li=&child_of=".$parent->post_parent."&echo=0");
    	} else {
    		$children = wp_list_pages("depth=1&title_li=&child_of=".$post->post_parent."&echo=0");
    	$children = wp_list_pages("depth=1&title_li=&child_of=".$post->ID."&echo=0");
    if ($children) {?>
    	<ul><?php echo $children;?></ul>
    <?php }?>

    When I’m on the lowest level, in depth 3, my subnav will display the parents and not the siblings.

    to create a workaround to the bug in both wp_list_pages and get_pages depth and hierarchy you can ad a meta_key to the selection with the position of the page: parent, child, grandchild and so on.
    that line should look like:
    $pages = get_pages('child_of='.$post->ID.'&sort_column=menu_order&meta_key=Child_Loc&title_li=');
    don’t forget to ad the meta_keys to the pages.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘wp_list_pages – only want children to top level’ is closed to new replies.