获取儿童页面 Wordpress

Cor*_*uet 2 php wordpress

我在 Wordpress 中有这个层次结构:

- Page
    - Child 1
    - Child 2
    - Child 3
        - Subchild 1
        - Subchild 2
    - Child 4
- Page
Run Code Online (Sandbox Code Playgroud)

我想做的只是显示Subchild页面,所以我这样做了:

<?php 
     wp_list_pages( array(
        'child_of' => $post->ID,
        'depth' => 2 
        'sort_order' => 'asc'
    ));
?>
Run Code Online (Sandbox Code Playgroud)

但它显示所有child页面而不仅仅是SubChild页面

谢谢你的帮助 !

Mar*_*arc 5

为了基于父页面显示子页面,我编写了以下代码:

在 childArgs 中,您提供参数,最重要的是 child_of,这里我说我想要具有此 ID 的页面中的所有页面。您可以使用 get_the_ID() 函数或从您的页面输入 id。

$childList我使用 get_pages 函数从特定页面 ID 返回数组中的所有页面。

比我在从 get_pages 函数获得的数组上使用 foreach 并显示内容和标题。如果您想单独设置子页面的样式,我使用 post_class() 函数将子页面名称作为类指定给它。

<?php
      $childArgs = array(
          'sort_order' => 'ASC',
          'sort_column' => 'menu_order',
          'child_of' => get_the_ID()
      );
      $childList = get_pages($childArgs);
      foreach ($childList as $child) { ?>
        <!-- Generates all class names you would expect from a normal page. Example: page, post-{id} etc. -->
        <section <?php post_class($child->post_name); ?>>

        <h1><?php echo $child->post_title; ?></h1>


        <?php echo apply_filters( 'the_content', $child->post_content); ?>

        </section>  

    <?php } ?>
Run Code Online (Sandbox Code Playgroud)

结果是: - 主页(什么都不显示) - subpage1(显示:标题,内容) - subppage2(显示:标题,内容)