Php Wordpress使用foreach循环在子页面上列出子页面

Php Wordpress使用foreach循环在子页面上列出子页面,php,wordpress,loops,foreach,Php,Wordpress,Loops,Foreach,我使用foreach循环在父页面上列出子页面,这非常有效。但是,子页面不在子页面上列出,这正是我想要的 到目前为止,以下内容适用于父页面: <?php $nav = array( 'post_type' => 'page', 'orderby' => 'title', 'order' => 'ASC', 'post_parent' => $post->ID ); $child_pages = get_posts($nav);

我使用foreach循环在父页面上列出子页面,这非常有效。但是,子页面不在子页面上列出,这正是我想要的

到目前为止,以下内容适用于父页面:

<?php

$nav = array(
    'post_type' => 'page',
    'orderby' => 'title',
    'order' => 'ASC',
    'post_parent' => $post->ID
);

$child_pages = get_posts($nav);

?>
<ul>
    <?php foreach ($child_pages as $child_page) : ?>
        <li>
            <a href="<?=get_permalink($child_page->ID); ?>"><?=$child_page->post_title; ?></a>
        </li>
    <?php endforeach; ?>
</ul>

理想情况下,我希望继续使用foreach而不是任何其他循环。我想知道是否需要在现有循环中添加另一个循环,但我不确定如何继续


如果您有任何帮助,我们将不胜感激。

请使用“家长”而不是“后家长”

    <?php

    $nav = array(
        'post_type' => 'page',
        'orderby' => 'title',
        'order' => 'ASC',
        'child_of' => $post->ID
    );

    $child_pages = get_posts($nav);

    ?>
    <ul>
        <?php foreach ($child_pages as $child_page) : ?>
            <li>
                <a href="<?=get_permalink($child_page->ID); ?>"><?=$child_page->post_title; ?></a>
            </li>
        <?php endforeach; ?>
    </ul>


希望它有助于

使用家长而不是事后家长

    <?php

    $nav = array(
        'post_type' => 'page',
        'orderby' => 'title',
        'order' => 'ASC',
        'child_of' => $post->ID
    );

    $child_pages = get_posts($nav);

    ?>
    <ul>
        <?php foreach ($child_pages as $child_page) : ?>
            <li>
                <a href="<?=get_permalink($child_page->ID); ?>"><?=$child_page->post_title; ?></a>
            </li>
        <?php endforeach; ?>
    </ul>


希望它有帮助

您可以在这里使用wp\u get\u post\u parent\u id检查当前页面是否为子页面-请参阅,然后如果该页面是子页面-使用父id,否则只需使用post id。类似的操作应该可以:

global $post;
if(wp_get_post_parent_id( $post->ID ))
{
    $nav = array(
        'post_type' => 'page',
        'orderby' => 'title',
        'order' => 'ASC',
        'post_parent' => wp_get_post_parent_id( $post->ID )
    );
}
else{
    $nav = array(
    'post_type' => 'page',
    'orderby' => 'title',
    'order' => 'ASC',
    'post_parent' => $post->ID
    );
}
$child_pages = get_posts($nav);

您可以在这里使用wp_get_post_parent_id检查当前页面是否为子页面-请参阅,然后如果该页面为子页面-请使用父id,否则只需使用post id。类似的操作应该可以:

global $post;
if(wp_get_post_parent_id( $post->ID ))
{
    $nav = array(
        'post_type' => 'page',
        'orderby' => 'title',
        'order' => 'ASC',
        'post_parent' => wp_get_post_parent_id( $post->ID )
    );
}
else{
    $nav = array(
    'post_type' => 'page',
    'orderby' => 'title',
    'order' => 'ASC',
    'post_parent' => $post->ID
    );
}
$child_pages = get_posts($nav);

嘿-不幸的是,这并没有给我那个家长的子页面。这给了我一个所有页面的列表。不过谢谢。我已经把家长改成了child_,如果有帮助的话,试试看——不幸的是,这并没有给我那个家长的子页面。这给了我一个所有页面的列表。谢谢。我已经把父母改成了孩子。如果有帮助的话,试试看