3 Level Navigation with Wordpress


/ Published in: PHP
Save to your folder(s)

This was for a customer.
Basically it does this:
If a page has no children, it shows ... nothing,
if a page has a parent, it shows all other children of this parent (siblings) and the current page
if a page has a parent AND children (3rd level), it shows two separate menus, one with the siblings and current page and one with the children of the third level.
Maybe it helps someone.


Copy this code and paste it in your HTML
  1. <?php
  2. //Automatic Submenu
  3. global $wp_query;
  4. //If current page has no parent...
  5. if( empty($wp_query->post->post_parent) )
  6. {
  7. echo "";
  8. }
  9.  
  10. else
  11. //current page has parents
  12. {
  13. $me=$wp_query->post->ID;
  14. $children = wp_list_pages("title_li=&child_of=$me&echo=0");
  15. if ($children)
  16. {
  17. $parent1 = $wp_query->post->post_parent;
  18. //show menu with current page's siblings
  19. echo "<ul class='submenus'>";
  20. wp_list_pages("title_li=&child_of=$parent1&depth=1");
  21. echo "</ul>";
  22.  
  23. $parent2 = $wp_query->post->ID;
  24. //show menu with current page's children
  25. echo "<ul class='subsubmenu'>";
  26. wp_list_pages("title_li=&child_of=$parent2&depth=1");
  27. echo "</ul>";
  28.  
  29. }
  30. else
  31. {
  32. $parent1 = $wp_query->post->post_parent;
  33. //show ONLY menu with current page's siblings
  34. echo "<ul class='submenus'>";
  35. wp_list_pages("title_li=&child_of=$parent1&depth=1");
  36. echo "</ul>";
  37. }
  38. }
  39. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.