Menu with siblings and all upper-level links
-
Hello,
I was asked to do a specific menu for a website and I have no idea how to do it.What I want :
– The menu is different depending of the page
– It shows the top-level page and all of the 2nd level pages
– It show all of the sibblings of the current page
Example here:
https://www.univ-lille3.fr/universite/presentation/
The menu at the left of the content is exactly what I need. There is a title which is the top-level parent, and then a list of the 2nd level pages, and when you enters a page, you see the childrens of that page. When on a children, you still see the sibblings.
As the menu is different depending the page we’re on (each top-page having its own subpages), I’m using php to generate that menu.Tested solution 1:
I got a code to get all of the subpages (on any level) of my top-level page. Now I have to find a way to hide the unwanted items and keep the one I want to keep.
I’m using this code:<?php if($post->post_parent) $children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0"); else $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0"); if ($children) { ?> <ul> <?php echo $children; ?> </ul> <?php } ?>
So I get a complete hierarchy of my top-page. Using display:none, I can hide the unwanted children, and using the current-item class, I can display the item I’m on.
Problem:
I don’t find a way to get the sibblings as well.
I was hoping for a CSS selector that would be like “every ul that containts a .curent-item” but it doesn’t exists. Maybe I should do some javascript to add a class to siblings or the ul containing curent-item but I have no idea how it works. I only know basis about such languages.Tested solution 2:
Another code I found on the codex (I can only copy/paste codes because I don’t know how to code)<?php $ancestor_id=6;//this code is wrong $descendants = get_pages(array('child_of' => $ancestor_id)); $incl = ""; foreach ($descendants as $page) { if (($page->post_parent == $ancestor_id) || ($page->post_parent == $post->post_parent) || ($page->post_parent == $post->ID)) { $incl .= $page->ID . ","; } }?> <ul> <?php wp_list_pages(array("child_of" => $ancestor_id, "include" => $incl, "link_before" => "", "title_li" => "", "sort_column" => "menu_order"));?> </ul>
Seems to work nice for the hierarchy, I have the needed pages. But I get a perfectly flat list. All items are li, and there is no inclusion. Children are not a sub-list. Then, my children are in the top of the list, with their parent below. I have no idea how to style this in a clean way to get the parents above, and the children listed below with a different style showing they’re children.
Tested solution 3:
function menu_rubrique($pageid=null){ global $post; if($pageid == null) $pageid = $post->ID; //Si il n'y a pas d'id, on récupère celui de l'objet. $current = get_page($pageid); if($current->post_parent != 0){ //Si il y a un parent $ancestor_id=$current->post_parent; $ancestor = get_page($ancestor_id); $descendants = get_pages(array('child_of' => $ancestor_id, 'sort_column' => 'menu_order, post_title')); $incl = ""; foreach ($descendants as $page) { if (($page->post_parent != $ancestor_id) && ($page->post_parent != $post->post_parent) && ($page->post_parent != $post->ID)) { $incl .= $page->ID . ","; } }?> <ul id='menu-rubrique'><li><a href='<?php echo get_permalink($ancestor_id); ?>'><?php echo $ancestor->post_title; ?></a> <?php wp_list_pages(array("child_of" => $ancestor_id, "exclude" => $incl, "link_before" => "", "title_li" => ""));?> </li></ul><!-- #menu-rubrique --> } }
I don’t even remember where I found this.
Problem:
This one give me a nested list, but the problem is that it changes when I go on a child page. I get the page’s siblings and children, but not the parents.So…. I tried several solutions but none of them give me the wanted result. As I don’t know much how coding works, I can’t edit the codes to get the result I want.
That’s why I’m asking to you. If you know the way to edit any of those codes to help me get what I want, or if you know any other code or solution that may help, I want it.Thanks a lot for any help.
Hoping my post isn’t too long…PS : I’m not English, then I do lots of mistakes when writing. I’m sorry for this. I hope everything is understandable.
- The topic ‘Menu with siblings and all upper-level links’ is closed to new replies.