Wordpress三级导航

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Wordpress三级导航相关的知识,希望对你有一定的参考价值。

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.
  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. ?>

以上是关于Wordpress三级导航的主要内容,如果未能解决你的问题,请参考以下文章

js+数据库生成三级动态tree导航菜单

WordPress - 代码片段插件

Bootstrap 4 三级子菜单不在右侧

简单实现三级导航栏

markdown 在WordPress中使用jQuery代码片段

Wordpress - 将代码片段包含到布局的选定部分的插件