wordpress查询菜单顶级父级的所有子级 [英] wordpress query menu all children of top level parent

查看:206
本文介绍了wordpress查询菜单顶级父级的所有子级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我迷路了:)我尝试了很多失败的解决方案.我需要编写代码来创建当前主要(顶层)父级的所有子级的菜单.页面结构如下:

I'm lost :) I tried many solutions byt I failed. I need to write the code to create menu of all children of current main (top level) parent. Pages structure looks like this:

  • 主页
  • A页
    • 子页面A1
    • 子页面A2
      • 子页面A2-1
      • 子页面A2-2
      • Homepage
      • Page A
        • Subpage A1
        • Subpage A2
          • Sub-subpage A2-1
          • Sub-subpage A2-2
          • 子页面B1
          • 子页面B2
            • 子页面B2-1
            • 子页面B2-2
            • Subpage B1
            • Subpage B2
              • Sub-subpage B2-1
              • Sub-subpage B2-2

              因此,如果我在页面A 子页面A2 子页面A2-1中...我想列出相同的元素:

              So, if I'm on Page A or Subpage A2 or Subpage A2-1 ... i want to list the same elements:

              • 子页面A1
              • 子页面A2
                • 子页面A2-1
                • 子页面A2-2
                • Subpage A1
                • Subpage A2
                  • Sub-subpage A2-1
                  • Sub-subpage A2-2

                  当前顶级父级(页面A)的所有子级并不表示我们的深度".有什么帮助吗? :)

                  All children of current top level parent (Page A), doesn't metter how 'deep' we're. Any help? :)

                  推荐答案

                  以下是答案:

                  <?php
                      if ($post->post_parent) {
                          $ancestors=get_post_ancestors($post->ID);
                          $root=count($ancestors)-1;
                          $parent = $ancestors[$root];
                      } else {
                          $parent = $post->ID;
                      }
                      echo '<ul>';
                      wp_list_pages('orderby=name&depth=4&order=DESC&show_count=0&child_of='.$parent.'&title_li=');
                      echo '</ul>';
                  ?>
                  

                  这篇关于wordpress查询菜单顶级父级的所有子级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆