php 如何在wordpress中获取父页面的所有子页面?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16488429/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:12:50  来源:igfitidea点击:

How to get all child pages of a parent page in wordpress?

phpwordpressparent-child

提问by krathos

Example:

例子:

About
--- Menu 1
--- Menu 2
--- Menu 3
--- Menu 4

if i'm in about page... i have the sub pages. but, if enter to Menu 1 all the pages disappear

如果我在关于页面...我有子页面。但是,如果进入菜单 1,所有页​​面都会消失

What i need is all the time see the parent pages

我需要的是一直看到父页面

Currently I have this code

目前我有这个代码

<? if (is_page()) {
    $g_page_id = $wp_query->get_queried_object_id();
    wp_list_pages("depth=4&title_li=&child_of=".$g_page_id."&sort_column=menu_order");
   }
?>

Thanks!

谢谢!

Resolved

解决

i use this and work fine!

我用这个并且工作正常!

<?php
if ( is_page() ) :
    if( $post->post_parent ) :
        $children = wp_list_pages( 'title_li=&child_of='.$post->post_parent.'&echo=0' );
    else;
        $children = wp_list_pages( 'title_li=&child_of='.$post->ID.'&echo=0' );
    endif;
    if ($children) : ?>
        <div class="title">
            <?php
            $parent_title = get_the_title( $post->post_parent );
            echo $parent_title;
            ?>
            <span></span>
        </div>
        <ul>
            <?php echo $children; ?>
        </ul>
    <?php
    endif;
endif;
?>

回答by JosFabre

Here you go. A bit late for the author, but people will come here for an answer still ;-)

干得好。作者有点晚了,但人们还是会来这里寻求答案;-)

<?php 
// determine parent of current page
if ($post->post_parent) {
    $ancestors = get_post_ancestors($post->ID);
    $parent = $ancestors[count($ancestors) - 1];
} else {
    $parent = $post->ID;
}

$children = wp_list_pages("title_li=&child_of=" . $parent . "&echo=0");

if ($children) {
?>

    <ul class="subnav">
        <?php 
            // current child will have class 'current_page_item'
            echo $children; 
        ?>
    </ul>

<?php 
} 
?>

回答by Daniel Morell

The easiest way to handle this is with get_children(). It pretty much does what you would expect. It returns the child pages of a parent page.

处理此问题的最简单方法是使用get_children(). 它几乎可以满足您的期望。它返回父页面的子页面。

get_children()is basicly a wrapper for the WP_Queryclass.

get_children()基本上是WP_Query类的包装器。

You can use it like this...

你可以这样使用它......

$child_args = array(
    'post_parent' => 1, // The parent id.
    'post_type'   => 'page',
    'post_status' => 'publish'
);

$children = get_children( $child_args );

If you want to return the children of the current post you can pass in $post->IDas the 'post_parent'.

如果你想返回当前帖子的孩子,你可以$post->ID作为'post_parent'.

Documentation for get_children()

文档 get_children()

Documentation for WP_Query

文档 WP_Query