wordpress查询菜单顶级父级的所有子级

时间:2018-02-22 18:35:08

标签: wordpress menu parent-child

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

  • 主页
  • 页面A.
    • 子页面A1
    • 子页面A2
      • 子页面A2-1
      • 子页面A2-2
  • Page B.
    • 子页面B1
    • 子页面B2
      • 子页面B2-1
      • 子页面B2-2
    • 子页面B3
  • 子页面C

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

  • 子页面A1
  • 子页面A2
    • 子页面A2-1
    • 子页面A2-2

当前顶级家长(页面A)的所有孩子都不会遇到“深层次”的问题。我们'重。有帮助吗? :)

1 个答案:

答案 0 :(得分:0)

以下是答案:

<?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>';
?>